3
Aug
3

Automatically resolving jars for the Java classpath

When you use the Java classpath (pre java 1.6) you have to manually list each jar on your classpath such as:

java -cp lib/database.jar:lib/commons.jar:/lib/log.jar com.joejag.Main

On Java 1.6 a little known feature is that you can now use wildcards, so the above command becomes:

java -cp lib/*.jar com.joejag.Main

I still have to use an earlier version of Java for some applications I handle. To save having to list all the jars manually I use the following bash script which allows you to automatically list all the jars in a directory:

java -cp `find lib -name *.jar -exec printf :{} ';'` com.joejag.Main
Enjoyed reading this post?
Subscribe to the RSS feed and have all new posts delivered straight to you.
3 Comments:
  1. Bruce 3 Aug, 2009

    Joe,

    Don’t I just want to use Ant instead of doing this jars in the command line stuff?

  2. Joejag 3 Aug, 2009

    Hey Bruce. For building your software you definitely want to use Ant (or another build tool) like you say.

    But for clients running your distributed software you probably won’t bundle the build tool with the application. So they will be using the plain ‘java’ command via a batch/shell script.

  3. Kevin McDonagh 6 Aug, 2009

    Yay, a tech blog!

Post your comment