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!
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.
Related
I built a java program in Eclipse in Windows and it worked well. The program included 5 java classes and 5 jar library files.
Then I copied all *.java files and *.jar files to Linux. After I compiled and run it, I got an exception, the class PaserException of htmlparser.jar was not found.
Exception in thread "main" java.lang.NoClassDefFoundError: org/htmlparser/util/ParserException
Caused by: java.lang.ClassNotFoundException: org.htmlparser.util.ParserException
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)
I just begin to learn Linux so I'm not sure if I compiled and run it correctly.
All of my files(*.java and *.jar) are in the same directory, and my operation is in this directory.
To compile:
javac -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar *.java
To run:(the main method is in Crawler class, main method requires at lease 1 argument)
java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar Crawler arg0 arg1
Then I got the exception above. Did I compile and run it correctly? Why did I got this exception? Thanks.
The folder that holds the class file (.) is not on the classpath. Add another :. to the classpath:
java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar:. Crawler arg0 arg1
The file FactoryDemo.java is compiled successfully and in it there is the
package home.city.Desktop.factorydemo;
I compile it by using javac -d / FactoryDemo.java and nothing wrong is reported.
But when I try to run it by using java home.city.Desktop.factorydemo.FactoryDemo, it reports:
Exception in thread "main" java.lang.NoClassDefFoundError: home/city/Desktop/factorydemo/FactoryDemo
Caused by: java.lang.ClassNotFoundException: home.city.Desktop.factorydemo.FactoryDemo
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: home.city.Desktop.factorydemo.FactoryDemo. Program will exit.
I have tried many times but I could not find the solution to the problem. Thanks for your help in advance.
Your compiled class must be in a directory called:
home/city/Desktop/factorydemo
On Unix, just run this:
mkdir -p home/city/Desktop/factorydemo; mv FactoryDemo.class home/city/Desktop/factorydemo/
For best practice, simply move your Java source file in that folder too so the compiler outputs the file to the right spot.
FactoryDemo Class is created under root directory, you should type following command ( . instead of /)
javac -d . FactoryDemo.java
It creates home.city.Desktop.factorydemo directories starting from current directory.
If you want class created under root directory. You must type following command to execute.
java -classpath / home.city.Desktop.factorydemo.FactoryDemo
I am trying to run this command on linux... compilation is successfull but while running it gives error.
Executing this command:
java -cp .:/smash/same/hope/ant-launcher-1.6.1.jar src.vp
output
Setting the value for property-debug
Fusion Repository/asd/file/repo
Logs Directory
Running validations
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
at src.vp.call(vp.java:114)
at src.vp.main(vp.java:172)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
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:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
... 2 more
Can some one help me to solve it...
You've included the ant-launcher jar file, but that doesn't contain the whole of ant, I'm sure. Look for the ant jar file that includes the Project class. (I'd expect it to be ant-1.6.1.jar based on the other file you've specified.)
Using Ant to compile simple Hello World program in Java, and something isn't working right. I know the Java code is correct, as it runs using javac. Something is wrong with my Any config file, and I can't quite figure out what it is. http://pastebin.com/q50L5b0D
and the command line errors I'm getting:
**Exception in thread "main" java.lang.NoClassDefFoundError: Proj0/class
Caused by: java.lang.ClassNotFoundException: Proj0.class
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: Proj0.class. Program will exit.
**
It just tell you the class Proj0 could not be found when trying to run your application using the ANT target you defined. Are you sure this line is correct ?
<property name="Proj0.class" value="package.names.Proj0"/>
The package.names.Proj0 is a strange name for a package, maybe you wanted something like using the actual value of a property called package.names?
For your javac task need to set the classpath , srcdir properly. As Vincent wrote you are missing the Proj0 class. Please look here for how to set the javac task.
my program is in package pl.edu.uj.tcs.crazySocket; due to some requirements. I work in the crazySocket directory. In order to compile the program I use command
CLASSPATH=~/prog/colosseum/data javac tictactoe.java
and that succeeds. I want to run the program. I change javac to java and get rid of the '.java'. I get
Exception in thread "main" java.lang.NoClassDefFoundError: tictactoe
Caused by: java.lang.ClassNotFoundException: tictactoe
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:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
when I get up in the tree directory to the ~/prog/colosseum/data and run java pl/edu/uj/tcs/crazySocket/tictactoe all works fine.
What's wrong with the CLASSPATH? Shouldn't it work both with javac and java?
There's nothing wrong with your classpath, it's your call to java that's wrong.
You need to specify the full qualified class name pl.edu.uj.tcs.crazySocket.tictactoe.