Classpath option works for javac but not for java

2019-09-19 08:18发布

问题:

Hi and thanks for reading,

after usually working with C# I tried writing some java code yesterday to process an XML file. I didn't use an IDE or anything, just Notepad++, SDK and a lot of Google :)

I needed to include some jar files for which I found out, that one needs to set the classpath as a command line parameter to find the jar file. That worked well:

javac -cp "./metadata-extractor-2.9.1.jar;./xmpcore-5.1.2.jar" DescribeIt.java

Although using the same syntax when running it with java.exe I had less luck:

java DescribeIt -cp "./metadata-extractor-2.9.1.jar;./xmpcore-5.1.2.jar" 

Leads to

Exception in thread "main" java.lang.NoClassDefFoundError: com/drewimaging/ImageMetadataReader at DescribeIt.main(DescribeIt.java:53)
Caused by: java.lang.ClassNotFoundException: com.drew.imaging.ImageMetadataReader
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

When I unzip the jar files, it works perfectly fine, but I guess that's not the way jar files are supposed to be used.

Code and build batch file can be found here

Thanks a lot,

Flo

回答1:

Ouch! The problem was the order of parameters (and adding the current directory):

java -cp "metadata-extractor-2.9.1.jar;xmpcore-5.1.2.jar;." DescribeIt