Finding Java Classes

I’m back on a Java project. Been a while, and I want to capture some of the tricks I’m using.

Right now, I’m just trying to import the project into eclipse.  Seems that the current team members don’t use it.  I’m an IDE kind of guy, at least when it comes to Java.

Building the .classpath file can be tricky.  However, since I know that I have a good build, and that this project it a good participant in the Fedora build process, I have the advantage of knowing that my packages reside in /usr/share/java.  Still, all eclipse gives me is a set of classes that it can’t find.  how to find them?

This project uses CMake.  I could look for all of the Jar files in the CMakeLists.txt files, and I might do that in the future.  However, a trick I’ve developed in the past has come in handy.

 

class2path(){
echo $1 | sed 's!\.!\/!g'
}

JDIR=/usr/share/java

make_alljars(){
for JAR in `find /usr/share/java -name \*.jar -type f ` 
	do for CLASS in `jar -tf $JAR |  grep \.class` 
		do echo $JAR $CLASS  
	done 
done > /tmp/alljars.txt
}

 

First, the make_alljars function creates a map in (value key) order. The value is the Jar file name, and the key is the class name. To fine a Jar file that contains a given class (in this example netscape.ldap.LDAPConnection) , run:

 

grep `class2path  netscape.ldap.LDAPConnection` /tmp/alljars.txt

And the output is

/usr/share/java/ldapjdk.jar netscape/ldap/LDAPConnection$ResponseControls.class
/usr/share/java/ldapjdk.jar netscape/ldap/LDAPConnection.class


This works really well with eclipse, in that the error messages have the name of the class. You can then just highlight the class name, paste it into the command line in place of the class I have above, and when you get the Jar file name, you can highlight to save to the clipboard. From The right click context menu pick Java Build Path and then Add External Archive and then paste the whole path in.

1 thought on “Finding Java Classes

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.