Fri 29 Dec 2006
A useful linux script for Java Heads
Posted by gremlin under Java
Comments Off
If like me you are a java developer on unix this little blog might be of some use if you have lots of jars and are prone to “which jar contains the class X” where X could be riomhaire.roster.model.User and you find your self in a long search. What I do is ….
Have a file in my home directory called “classlist.txt” which contains the output of “jar -t” after some text replacements to remove non classes and to replace “/” with “.” so we can get the class names correct. Each jar dump includes the jar name and location.
The script is fairly simple being :
#####################################################################
echo “##### ${1} ####”
jar -tf ${1} | sed ’s/\//./g’ | grep “\.class$”
echo “”
#####################################################################
and which I call ‘listJarClasses’
I invoke the script via the shell command line:
find / -name “*\.jar” -exec listJarClasses {} >>classLocations.txt \;
at the end I end up with a file which can look like:
##### ./ant/etc/ant-bootstrap.jar #### org.apache.tools.ant.Main.class ##### ./ant/lib/ant-antlr.jar #### org.apache.tools.ant.taskdefs.optional.ANTLR.class
##### ./ant/lib/ant-apache-bcel.jar #### org.apache.tools.ant.filters.util.JavaClassHelper.class org.apache.tools.ant.util.depend.bcel.AncestorAnalyzer.class org.apache.tools.ant.util.depend.bcel.DependencyVisitor.class org.apache.tools.ant.util.depend.bcel.FullAnalyzer.class
……
Then if I want to find a class I open the file and search for the class name I am looking for and look for the preceeding jar name.
Hope this is of some use.