John Hoffmann's Weblog

All | AI | Comedy | Cool Threads | General | Java | Open Source | Robotics | Solaris 10 | Wiki
« Previous day (Dec 1, 2008) | Main | Next day (Dec 3, 2008) »

20081202 Tuesday December 02, 2008

java classpath unix shell tip

I am writing a java web service client as a command line tool. I'm using a 3rd party stub that depends on Apache Axis2 libraries. When it came time to invoke the java program, I realized I needed to load up the classpath with all 59 jars that are in Axis2. So, I came up with this little diddy:

java -cp `ls -1 /Projects/xyz/lib/axis2-1.4.1/lib/*.jar | sed 's/\(.\)$/\1:/' | tr -d '\n'`/Projects/xyz/build/lib/Xyz32-test-client.jar sun.rre.get.GetFolderList

Let's break it down:

javainvoke JVM
-cpuse the following word as the classpath
use the backticks to execute shell commands:
`starting backtick
ls -1 *.jarthat is a one, not an ell - list all jars one per line
| pipe the output to the next command
sed 's/\(.\)$/\1:/'replace the last character on the line with last character plus a semi-colon
|pipe the output to next command
tr -d '\n'remove the newlines from stream
`closing backtick
/Projects/xyz/build/lib/Xyz32-test-client.jarmy jar file appended to end of the output from backticks
sun.rre.get.GetFolderListthe class to invoke from my jar

The unix shell is still a source of joy to me almost on a daily basis after 11 years at Sun.

(2008-12-02 13:19:22.0) Permalink Comments [2]