I am trying to get a piece of code working with log4j. If I run it via junit tests (ant task), I am getting proper log outputs and all is fine. If I run the code from the command line, I get this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout
at uk.co.bytemark.flexnbd.Main.main(Main.java:29)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
I tried the two following classpaths but neither helped. The error remains.
export CLASSPATH=/home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar
export CLASSPATH=/home/yann/java/apache-log4j-1.2.16/
Even with this
$ java -cp /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar -jar ./dist/lib/flexnbd-`date +%Y%m%d`.jar server ::1 12345 fileSystem
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layout
at uk.co.bytemark.flexnbd.Main.main(Main.java:29)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
$ ls -ld /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar
476K -rw-r--r-- 1 yann yann 471K Mar 31 05:16 /home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar
Any idea what I am doing wrong?
Found the problem. If you use -jar, your CLASSPATH is ignored. A better command line is thus:
java -cp dist/lib/flexnbd-20100716.jar:/home/yann/java/apache-log4j-1.2.16/log4j-1.2.16.jar uk.co.bytemark.flexnbd.Main [...]
Thanks for the help!
Related
java -cp /home/gjhawar/kafka/core/src/main/scala/examples TestProd.class
Exception in thread "main" java.lang.NoClassDefFoundError: TestProd/class
Caused by: java.lang.ClassNotFoundException: TestProd.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: TestProd.class. Program will exit.
Why am i getting this error even thought the Class file is there in this directory .. Do I have to include the path to the jar files too in java command?
Your call is invalid. Replace TestProd.class with TestProd.
not sure about what the package you declare in TestProd, I guess you should run it by:
java -cp /home/gjhawar/kafka/core/src/main/scala examples.TestProd
and if TestProd is written in scala, you have to include scala-library.jar in your classpath
If your code file is HelloWorld.java then
To compile : javac [-options] <path>/HelloWorld.java
To execute : java [-options] <path>/Helloworld
I seem to be unable to compile a simple MongoDB + Java Example:
I have this file
https://github.com/mongodb/mongo-java-driver/blob/master/examples/QuickTour.java
In my command line I compile by doing
$ javac -cp mongo-2.10.1.jar QuickTour.java
$ java -cp mongo-2.10.1.jar QuickTour
However it gives me the error
Exception in thread "main" java.lang.NoClassDefFoundError: QuickTour
Caused by: java.lang.ClassNotFoundException: QuickTour
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
What is going on and how can I fix this? Is it something to do with my compilation arguments?
Thanks...
It's not the compile time error. It's the error you got when you try to run your class. And the error simply says that it cannot find the class that you are trying to run. That error almost always means that you messed up with the classpath.
The problem is, you forgot to include the current directory in your argument to classpath. Add a dot(.) in addition to your mongo.jar file as the argument to -cp:
java -cp .;mongo-2.10.1.jar QuickTour
This assuming that you are executing your QuickTour class from the same directory where you have placed it.
When I run a Java program with a command line option -DsocksProxyHost 10.123.76.20, I got an error :
java.lang.NoClassDefFoundError: 10/123/76/20
Caused by: java.lang.ClassNotFoundException: 10.123.76.20
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: 10.123.76.20. Program will exit.
Exception in thread "main"
-D should be used as follows:
-DsocksProxyHost=10.123.76.20
It would be helpful if you put on your command line here.
To run a Java program from command line, you have to specify the class which you would like to be invoked.
Let's say your program is located in c:\program\my.jar and com.myapp is the full name of the class you want to run, you should do like this:
java -cp c:\program\my.jar com.myapp -DsocksProxyHost 10.123.76.20
I'm trying to to run my freshly installed apache solr on my server (CENTOS 5.2 x86_64) and I get the followoing error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: start/jar
Caused by: java.lang.ClassNotFoundException: start.jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: start.jar. Program will exit.
This is the commend I ran: java start.jar
As far as I'm aware everything is installed correctly.
Any ideas?
Thanks
C
Run it with java -jar, not java:
java -jar start.jar
The java command is used for running .class files, while the java -jar command actually executes the code in the jar properly, using the manifest file to interpret the jar's contents.
MyClassWithMainMethod.java uses classes of someJar.jar.
If I call:
java -cp someJar.jar MyClassWithMainMethod
I get the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: MyClassWithMainMethod
Caused by: java.lang.ClassNotFoundException: MyClassWithMainMethod
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
But when I set the CLASSPATH to my jar manually
export CLASSPATH=:/path/to/someJar.jar
it works calling
java MyClassWithMainMethod
What am I doing wrong?
What about
java -cp /path/to/someJar.jar MyClassWithMainMethod
If you don't give Java the complete path to the jar file, how do you expect that it would find it?
OK well the argument you give to "-cp" is the same sort of thing you use with the CLASSPATH variable - what happens when you do this:
java -cp .:someJar.jar MyClassWithMainMethod