Ever wish you could set CLASSPATH=c:\someDir\*.jar ?





6
Date Submitted Fri. Apr. 7th, 2006 9:50 AM
Revision 1 of 1
Helper jpinkham
Tags CLASSPATH | DOS | Java
Comments 2 comments
Here's a nifty trick I often use for testing.

Lets say you have a c:\foo\lib directory full of jarfiles for log4j, javamail...whatever.

Modern J2EE containers do a nice job of letting you deploy a jarfile with your code and have a lib directory full of jarfiles like this, but what if you just want to run a quick client from the command line?

Here's what you do:

package my.package;

import org.whatever.*;
import org.whateverElse.*;   // you can end up with a lot of jarfile dependancies

public class MyClass {
   public static void main(String[] args) throws Exception {
      // ... code uses whatever & classloader needs those jarfiles
   }
}

Normally, you would run it like this:

java -cp c:\foo\lib\whatever.jar;c:\foo\lib\whateverElse.jar ... my.package.MyClass [args]

This can get tedious listing every jarfile, especially if they are all in the same directory anyway.  I've always thought you should just be able to say -cp c:\lib\*.jar.  That doesn't work, but here's how to get the same effect:

java -Djava.ext.dirs=c:\foo\lib my.package.MyClass [args]
 
There are drawbacks to using this technique in production systems, but it's nice for quick development. You can also list more than one directory, separated like CLASSPATH elements with either : or ;.

Jim Pinkham

Comments

Comments IDE Development
Thu. Sep. 21st, 2006 10:28 PM    Beginner java_junkie
Comments Drawbacks
Fri. Jul. 14th, 2006 6:14 AM    Beginner SurfMan

Voting