Re: Java command line classpath wildcards

Discussion in 'Java' started by Tom Anderson, Dec 13, 2008.

  1. Tom Anderson

    Tom Anderson Guest

    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.


    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
    Tom Anderson, Dec 13, 2008
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gabe
  2. Thomas Kellerer
    Apr 13, 2005
  3. John B. Matthews

    Re: Java command line classpath wildcards

    John B. Matthews, Dec 12, 2008, in forum: Java
    John B. Matthews
    Dec 12, 2008
  4. Lew
  5. Lew