Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: Java command line classpath wildcards

Reply
Thread Tools

Re: Java command line classpath wildcards

 
 
Tom Anderson
Guest
Posts: n/a
 
      12-13-2008
On Fri, 12 Dec 2008, Spud wrote:

> After much misery, I discovered the solution to a problem using wildcards in
> the classpath on the java command line. I post it here just in case someone
> else has this problem and is lucky enough to type the right words into Google
> to find it.
>
> This does not work:
>
> java -cp lib/* MyClass
>
> JDK 1.6 supports wildcards on the classpath. If the lib directory contains a
> bunch of jar files, they won't get picked up correctly with this line. On my
> system I get this error:
>
> "Exception in thread "main" java.lang.NoClassDefFoundError:
> lib/commons-collections-3/2/jar"
>
> (That's one of the jar files in the lib dir).


What's happening is that the wildcard is being expanded, just as it would
be in any command line. Under windows, this is done by the application, or
rather the standard C library, as a standard part of startup; on unix, it
would be done by the shell.

But either way, the command after expansion looks like:

java -cp lib/some-library.jar lib/commons-collections-3.2.jar lib/some-other-library.jar MyClass

The first library path is absorbed by the -cp flag, and becomes the
classpath. But the subsequent ones aren't, and so java interprets them, or
at least the first one, as the name of a class to run. I don't know what
it makes of the slash, but those dots look like class name element
separators, so it looks for a class called jar, in a package
lib.commons-collections-3.2. It doesn't find it, and it quits with the
error message you describe.

> This works:
>
> java -cp lib/*; MyClass
>
> With the semicolon all is well. That little semicolon cost me half a day
> of work. Note that the semicolon is used on Windows; it's probably a
> colon on Unix, but I haven't tried it.


I doubt a colon would make it work on unix - i don't think the shell's
wildcard expansion takes any notice of it. I'm quite surprised this works
under windows, in fact, but then there, expansion is handled by the app
not the shell, so it's possible that java.exe is doing something unusual.

Under unix, you'd just escape the asterisk:

java -cp lib/\* MyClass

And i suspect something similar would work under windows too.

tom

--
Everyone has to die sooner or later, whether they be killed by germs,
crushed by a collapsing house, or blown to smithereens by an atom bomb. --
Mao Zedong
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Java classpath question and how to run a program from command line? Lew Java 1 11-29-2011 08:08 PM
Re: Java classpath question and how to run a program from command line? Lew Java 0 11-28-2011 11:58 PM
Re: Java command line classpath wildcards John B. Matthews Java 0 12-12-2008 09:50 PM
java -jar ignore the classpath command line argument Thomas Kellerer Java 5 04-13-2005 12:01 PM
while using javac -classpath some.jar some.java (Where does classpath get stored?) Gabe Java 3 08-27-2004 07:02 PM



Advertisments
 



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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57