Tuesday, December 9, 2008

Java classpath - adding libraries and jars

Setting/Using the Java classpath always brings up some trouble, especially when a lot of external libraries or JARs are needed.

Well, here is something to make life easier. It is apparently new with JDK 1.6: the wildcard character. It adds to the classpath ALL JARs, classfiles, zipped libraries etc. inside a particular directory.

To compile a Java file that needs a long list of JARs or external classes, try:
> javac -cp "path-to-library-folder/*" MyJavaClass.java
  • NOTE: Don't forget the quotes

To run a Java class that needs a long list of JARs or external classes, try something like:
> java -cp ".;path-to-library-folder/*" MyJavaClass
  • NOTE: Don't forget to include the current directory by putting "." in the class-path
  • NOTE: Don't forget the quotes again

No comments: