Search code examples
javacompilationclasspathjavac

Running multiple Java classes


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!


Solution

  • 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.