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.

Comments:

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 06:12 AM 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 04:18 AM PST #

Post a Comment:
Comments are closed for this entry.
About

hoffie

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today