Script to build a Java classpath

2008-05-13

If you need to compile or run Java programs from the command line, it can be a real hassle to identify all the jar libraries the program requires and include them in your classpath.

Here is a short script that will do this work for you on Unix, Linux or Mac systems. It finds all the jar files in the current directory or its subdirectories, and merges the list into a classpath string delimited by colon (:) characters.

export CP=`find . -name '*.jar' | tr "\n" :`

You can then run your program like so:

java -classpath $CP MyProgram
Tags: Coding Tips

Comments