Search code examples
javaclasspathglob

java classpath is finding a library unexpectedly


Suppose any trivial use of joda such as

package com.domain.testPackage;
public class MyObject
{
    public static void main(String[] args)
    {
        System.out.println((new org.joda.time.DateTime()).toString());
    }
}

The jar can be exported from Eclipse (Neon) via

File>Export>Java/Runnable Jar>Next>"Copy required libraries into a sub-folder"

Both of the following invocations of java will run.

java -cp testProject.jar:testProject_lib/'*' com.domain.testPackage.MyObject

java -cp testProject.jar com.domain.testPackage.MyObject

It seems that only the first run is correct. Why does the second invocation run?

BTW: Note the java wildcard * should be quoted so that Linux does not expand the wildcard. Instead the wildcard is passed verbatim to java and takes the Java-specific meaning that is "all the JAR files". Note that it also works without the quotes. I said you should quote it, not that you need to. It works because Linux glob is unlikely to find a file name that, among other specifics, has a colon in the middle like this testProject.jar:testProject_lib/* and a side-effect of making zero matches is that glob will conveniently (or bizarrely?) echo the whole token and java will see the echoed token and interpret it in Java-fashion.


Solution

  • Class-Path: . testProject_lib/joda-time-2.9.2.jar

    That was found in the manifest. That explains it.