0

I am trying to run a code with multiple Java class files and a jar file which is from a library I downloaded. I compiled them with the following:

javac -cp "quickfixj-all-.jar" BTCCMarketDataRequest.java Bot.java

The Bot class has the main method and the BTCCMarketDataRequest file has a bunch of other methods in the class. I am not creating any packages.

How should I run it though? If I do: java Bot I get the following output:

Exception in thread "main" java.lang.NoClassDefFoundError: quickfix/Group
    at Bot.main(Bot.java:4)
Caused by: java.lang.ClassNotFoundException: quickfix.Group
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    ... 1 more

The compiled class (the one in the jar file could not be found, but why? I compiled it.

I am new to Java so I have no idea what's going on.

Thank you!

skaffman
  • 398,947
  • 96
  • 818
  • 769
Luis Cruz
  • 1,488
  • 3
  • 22
  • 50

1 Answers1

0

You need to add the classpath while executing the program as well.

java -cp .:quickfixj-all-.jar Bot

This assumes that the Bot class is in the default package and all the jar and .class dependencies are in the same directory.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Thanks! What if there are multiple jar files? – Luis Cruz Feb 21 '15 at 08:48
  • You have to list all the jar files then. To avoid that the main class is usually bundled into its own jar file which lists all of its dependencies using /META-INF/Manifest.mf file. – Ravi K Thapliyal Feb 21 '15 at 09:02
  • Ok cool. How do you specify multiple jar files? Do I seperate them with a semicolon or something? – Luis Cruz Feb 21 '15 at 09:34
  • 1
    With Java 1.6 and later you can use wildcards when specifying a classpath. http://stackoverflow.com/a/1237107/611819 – dnault Feb 21 '15 at 09:37
  • @dnault Thanks for the share. Luis if you're on Java 6+, can you try `java -cp .:./* Bot` and see if you're able to reference multiple jars? – Ravi K Thapliyal Feb 21 '15 at 09:51