javamavenjarcaliper

java not finding class in jar after successfully compiling with javac against same jar


I was looking at this question on stackoverflow and thought I'd try to run the benchmark myself. I downloaded caliper and compiled it successfully using maven. I then compiled HashCodeBenchmark from the linked question successfully using the jar that maven created. However, when I try to run HashCodeBenchmark, java complains that it cannot find a entrypoint class inside the jar.

I am utterly perplexed and have no idea what the problem is.

compile:

$ javac benchmark/HashCodeBenchmark.java \
    -classpath caliper-1.0-beta-SNAPSHOT-all.jar \
    && echo success
success

run:

$ java benchmark.HashCodeBenchmark -classpath caliper-1.0-beta-SNAPSHOT-all.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/caliper/runner/CaliperMain
    at benchmark.HashCodeBenchmark.main(HashCodeBenchmark.java:64)
Caused by: java.lang.ClassNotFoundException: com.google.caliper.runner.CaliperMain
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

Solution

  • You're providing the -classpath argument after the class name - so it's just being passed to your main method. You need to provide it before the class name, and include the current directory in the classpath too:

    $ java -classpath caliper-1.0-beta-SNAPSHOT-all.jar:. benchmark.HashCodeBenchmark