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]
hi,
That's interestign and nice. I've never actually understood why java doesn't come with the possibility of a filepath wildcard, when setting a classpath. For newbies it would make things a lot easier.
But.. what I usually do, is set a main class to the jar, and along with it set a relative classpath for the jar. Then I can run my classes from the command line in a very simple short hand java -jar some.jar as long as all the jar files on the classpath are in the directory relative to where I execute the jar file, it works.
If I use maven to build my jar file, it can do this with very little configuration..
kindly -r
Posted by reynir on December 02, 2008 at 02:12 PM PST #
With JDK6 you can use classpath wildcards instead:
http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html (check out section Understanding class path wildcards)
cheers,
Igor
Posted by Igor Minar on December 05, 2008 at 12:18 PM PST #