Delete all .java files that don't have a related .class file

Hi,

I am looking for a script/method that can delete all java files, which do not have an associated .class file.

I have a huge java package of 225 java-files but I do only need 206 of them. I found this out by counting the associated .class files after compilation. Now I would like to delete all .java files that have no related .class file. Since it is a lot of work to go through all 225 files, which are spread out in many folders and subfolders, I am searching for a script, which can do this automatically.

Does anyone have an idea?

Cheers,
exopikus

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Dirty hack of the day:

for i in ls *.class ; do echo $i | sed -r ‘s/(.*).class$/\1.java/’ |
xargs rename java java.save; done ;rm *.java; rename java.save java *;

Good luck.

exopikus wrote:
> Hi,
>
> I am looking for a script/method that can delete all java files, which
> do not have an associated .class file.
>
> I have a huge java package of 225 java-files but I do only need 206 of
> them. I found this out by counting the associated .class files after
> compilation. Now I would like to delete all .java files that have no
> related .class file. Since it is a lot of work to go through all 225
> files, which are spread out in many folders and subfolders, I am
> searching for a script, which can do this automatically.
>
> Does anyone have an idea?
>
> Cheers,
> exopikus
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJKKNFAAAoJEF+XTK08PnB5dhAP/iSYMuuhX4nmvuxy1F6vTFF+
gqQY2Wcew7QRkMcyG85geT0Lxj9VfsppCnfcW8/ddclvpnI0RmGD9u5b/5/dmV6W
EF8Z3sdejJqiF5MAtJKdGB6ek6EYcuofu+3HOS+YuEFjgZBoUEunj9pB+mGgh8Us
WZ/maQtbmbP/XQCfXCPi2TlW/KTsC0IO9MrTjeKLl8LGBwgCKNqYMNFAJx4NMyHb
HeLLNOjQ+hWu/+U7rc3J5+RZRzB2FtRFMtMGOgldLYOGkO9yb/wqYl7s2TG6EES4
mtg66tbF9L5s/jBJ633Bn4D/8SfsikbLTV7OIOi47gJi3C/Gif7c/1KlkStAmPoV
dzvOn08kWDzVHnSOmUuwGm2MBPCsDSJzTDCpjbXdl4g4Jbt7x3Pf+A28gB4fxBQr
+kKYcFoXFA+ocTw5Zkug/+6eJ2h826AamZwlYJKF9A87CgGddhwhJE7AMsaVy1az
p/wyRTcnkZbD5piD8ybde5rHcLB8/4J2PerJv7H8ONBfInN7AJw7l3fIekkZdE9z
dMDq/oBAigPAHGR/FhOyLaD7Rzf4YOH9+Wjafd9lvce0bhgvvxbR/VSHlJ0S3zzT
uZiCWKKVW1p6lDCknlWcgwfuZt8UrcK4QnELKSeZw0R91zT6tE/td9uZ/mnrm4B6
4GBGgGjyKtNvjFM99dB/
=EBWJ
-----END PGP SIGNATURE-----

Thank you! How can I use this? Just writing it in in the terminal doesn’t work. I don’t have any experience in doing such things and I’m new in Linux.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Put it all on one line in a terminal after you have changed to the
directory where you have all of your *.java and *.class files. This
assumes they are all in the same directory.

Good luck.

exopikus wrote:
> ab@novell.com;1995260 Wrote:
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Dirty hack of the day:
>>
>> for i in ls *.class ; do echo $i | sed -r ‘s/(.*).class$/\1.java/’ |
>> xargs rename java java.save; done ;rm *.java; rename java.save java *
> Thank you! How can I use this? Just writing it in in the terminal
> doesn’t work. I don’t have any experience in doing such things and I’m
> new in Linux.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iQIcBAEBAgAGBQJKKNbZAAoJEF+XTK08PnB5sBEQAJ5gxM8oQFeiIiuI7xEfZPXL
JH6NJltfPcrCFJX7tI8IlK3MidQjj2AfVf73dvXA9MDqCr9bDSdLMc3kFOSKVgpt
3ZQvsqDFo8r9DsFyXeNMBk8uOanl9cX8Ib9hxtCSPMaMt37utpqdFNOI5N0I2AWO
/zJe7YmbKI0GsxBuGR5PGPx0q9CUavlb/kTh+7jUiNeo1dDAOQs7lwxx9h9xqGoT
lQNHj5H3r7FWUEYp8TPZYHAKDxeNeUwiIs2CJk2rOWLuC7+AC3F3l5Qp9/T2jkcM
dS0O17Iua5Z/rcyb0gAVGx7lD+mrnP4Sb+LvkIZRcJdreloDnyO1T3QDb9rCm0DV
CbSx1sHu2LUjiA4E0ltx+Eeju7MNh/qccCd3rSZsF7k8qhwND8TW92w8F2ZFu6J6
6SFihdmLHQjSn+RxDZZJ64y3bqkaTIN1e0HbSc9O8iPJNBBRO2D8fdee1lzEbtOT
b4LphADpFa+v1R9kxnQFnB0gHEqxkPEXqBbSBafy5GFzhMMW1cGc8geAU9xUwWoG
J7dZmRLZB8bkpg4aRXRzfoGYO0UJAEV42+k9RBBisV177z7/yzXKjsm/1kcmCTlH
KiO8lGJ72CICDJEFUk69e4eLe6mX0jlYmEomBuWjMO85yoIyz7akwu9rS5dB/hdF
RiuYqSAskO2jW+BMx9Vd
=Uhqo
-----END PGP SIGNATURE-----

OK. So it doesn’t work because all the files are distributed in many subfolders and subsubfolders…

no need to use ls…its useless here. use shell expansion


for i in *.class

You probably want to take a more organised approach to this.

Usually the .class file hierarchy mirrors the .java file hierarchy but in a different tree. So something like:

com/example/Module.java

might compile to:

class/com/example/Module.class

So what you want to do is to get a list of all the .class files, make the appropriate text substitutions en masse to get the .java file list. Those are the ones you want to keep. Now do a comm(1) against all the existing .java files and the ones not on the list are the ones you want to discard.

And backup of course.

here’s a Python script


#!/usr/bin/env python
import os
CLASS=]
classpath = os.path.join("/path","where","class","is")
javapath = os.path.join("/home","java") 
for r,d,f in os.walk(classpath):
    for files in f:
        if files-6:] == ".class":
            CLASS.append(files:-6])            
for r,d,f in os.walk(javapath):
    for files in f:
        if files-5:]==".java" and files:-5] not in CLASS:
            try:
                os.remove( os.path.join( r, files) )
            except Exception,e:
                print e
            else:
                print "%s removed" % os.path.join( r, files)

I am wondering how someone can reach such a situation! In any Java development environment, you don’t scatter your .java files just like that. Also, how come you really wrote something that never wanted to compile to a class file? Also, if you do not have a development environment like eclipse or even an simple ant or make script, how were you writing these programs? The situation is more complicated if you doing any dynamic class loading - in that case, you can’t just determine the files to be deleted.
Anyway, it is all up to you but definitely, there is something wrong with the way you are managing your projects (I am assuming that you not just a student only but, even then, you have to manage it better).

Thank you all!

@ghostdog74: How can I run this?

@syampillai: I didn’t write all these java files. I use a free package and I don’t need all of its classes. So I want to delete the unnecessary classes. I hoped I can understand the code after it’s smaller. Normally I use kate as my editor. However, eclipse is installed, too. And yes, I am just a student, not a professional :sarcastic:

save the code as myscript.py, and on command prompt


# python myscript.py

Thanks. That’s exactly what I did. However, it doesn’t work. All java files are still there. I don’t get an error message.
Maybe the problem is that all classes are organized in different packages / subfolders? So I also tried

python script.py -R
python -R script.py

That doesn’t work, too. :frowning:

i don’t like it when people say it doesn’t work, but don’t show why it doesn’t work. I have to ask you back are there any other error messages? where is your code ?


#!/usr/bin/env python
import os
CLASS=]
classpath = os.path.join("/path","where","class","is") # <--- makee sure you change this 
javapath = os.path.join("/home","java")  ##<----this too.
for r,d,f in os.walk(classpath):
    for files in f:
        if files-6:] == ".class":
            CLASS.append(files:-6])        
print CLASS # <---- add this line     
for r,d,f in os.walk(javapath):
    for files in f:
        print files-5:] # <----------add this and see for yourself
        print files:-5] #<====------- add this tooo
        if files-5:]==".java" and files:-5] not in CLASS:  ##<<----- make sure this is the logic you want
            try:
                os.remove( os.path.join( r, files) )
            except Exception,e:
                print e
            else:
                print "%s removed" % os.path.join( r, files)

run the above again and see the output.
Check my sig for learning Python.