Java classpath template behavior

I have used the template extension feature java -classpath

earlier and successfully. I am currently experiencing a strange problem.

The wildcard must expand to every one jar

in the named folder. Here is a quote from Oracle:

Class path entries can contain the basename wildcard character *, 
which is considered equivalent to specifying a list of all the files 
in the directory with the extension .jar or .JAR. For example, the 
class path entry foo/* specifies all JAR files in the directory 
named foo. A classpath entry consisting simply of * expands to a 
list of all the jar files in the current directory.

      

This is a link to Oracle doc on Java 6 on the subject of classpath.

The behavior I see contradicts this. Here are 3 runs. The first one explicitly names jar

and so it works. Others use wildcard and fail. What for? This matters to me because I rely on wildcards (elsewhere) and therefore understanding this unexpected behavior is very important to me.

#!/bin/bash

printf "The EV is...\n"
echo $CLASSPATH
printf "The working directory is...\n"
pwd
printf "Directory listing...\n"
ls 
printf "END of directory listing.\n"

printf "Test with named jar.\n"
java -javaagent:../sizeof/sizeof.jar -classpath ./testsizeof.jar info.zqxj.test.Tester

printf "Test with star.\n"
java -javaagent:../sizeof/sizeof.jar -classpath * info.zqxj.test.Tester

printf "Test with dot slash star.\n"
java -javaagent:../sizeof/sizeof.jar -classpath ./* info.zqxj.test.Tester

      

Output:

The EV is...

The working directory is...
/home/b/Documents/workspace/testsizeof
Directory listing...
bin  run.sh  src  testsizeof.jar
END of directory listing.
Test with named jar.
40
Test with star.
Exception in thread "main" java.lang.NoClassDefFoundError: run/sh
Caused by: java.lang.ClassNotFoundException: run.sh
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: run.sh.  Program will exit.
Test with dot slash star.
Exception in thread "main" java.lang.NoClassDefFoundError: //run/sh
Caused by: java.lang.ClassNotFoundException: ..run.sh
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: ./run.sh.  Program will exit.

      

+3


source to share


1 answer


Solution, specify the classpath argument twice. Example: -classpath "*"

This is needed both on the command line and inside a bash script.

Next addition:



Also, please note that -classpath "~/folder/*"

does not work but -classpath ~/folder/"*"

is good. Suggest a template, but don't quote ~

. You seem to need the operating system to interpret ~

, but you need to specify the pattern *

because you want to pass it in java

for extension in Java fashion.

Please note that you should not query java

for an extension *.jar

because it will lead to unintended results. The Java Specification says that the correct wildcard is only *

.

+5


source







All Articles