Friday, November 11, 2011

UNIX/Linux : Provide jar files in classpath at run time

You may face an issue to provide .jar files in classpath at run time.

Lets say you have a Java file Test.java which requires foo.jar and bar.jar in classpath.

In Windows below mentioned command works well.

javac -cp foo.jar;bar.jar Test.java
java -cp foo.jar;bar.jar Test

Note the semi-colon(;) as jar file separator in windows.

Where as in UNIX/Linux it don't work. To run the above program in UNIX/Linux you need to use below mentioned command

javac -cp foo.jar:bar.jar Test.java
java -cp foo.jar:bar.jar Test

Note the colon(:) as as jar file separator in UNIX/Linux.