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.
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
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
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.
#!/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).
@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:
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
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.