Scripting Hack to auto-fill classpaths

How to add a load of jars recursively below your current directory to the classpath without pain, unless you like pain?

First you use 'find' to generate a list of jars

For example...



find . -name '*.jar'

lib/java/saxon/saxon8-dom.jar

lib/java/saxon/saxon8-dom4j.jar

lib/java/saxon/saxon8-jdom.jar

lib/java/saxon/saxon8-sql.jar

lib/java/saxon/saxon8-xom.jar

lib/java/saxon/saxon8-xpath.jar

lib/java/saxon/saxon8-xqj.jar

lib/java/saxon/saxon8.jar

        

Then you pipe the result through something which first turns newlines into colons, then chops the trailing colon off - this chain.

tr '[:space:]' ':' | sed -e 's/:$//g'

The output of this, you can use as part of a java invocation, by incorporating the output into the command using backticks such as...

java -cp `find . -name '*.jar' | tr '[:space:]' ':' | sed -e 's/:$//g'` net.sf.saxon.Query

Alternatively you can output the result into a file, and incorporate it using cat, which is a bit shorter and more consistent.

find . -name '*.jar' | tr '[:space:]' ':' | sed -e 's/:$//g' > classpath
java -cp `cat classpath` net.sf.saxon.Query

Tagged:

scripting (6)

java (2)