Tuesday December 02, 2008 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:
| java | invoke JVM |
| -cp | use the following word as the classpath |
| use the backticks to execute shell commands: | |
| ` | starting backtick |
| ls -1 *.jar | that 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.jar | my jar file appended to end of the output from backticks |
| sun.rre.get.GetFolderList | the 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]