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.
Related
I have the following directory structure:
somedir/
lib/
myapp.jar
commons-io-2.1.jar
...lots of otherjars
From inside somedir, I try to run the following command:
java -cp lib/* net.myapp.Driver /home/myUser.blah.text
And I am getting the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: lib/commons-io-2/1/jar
Caused by: java.lang.ClassNotFoundException: lib.commons-io-2.1.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: lib/commons-io-2.1.jar. Program will exit.
Why is it telling me that it can't find a "main" in Apache Commons IO? I might be able to understand if it couldn't find my Driver class (maybe I misconfigured something), but this is just throwing me way off. Ideas? Thanks in advance!
From the stack trace it appears as if java is taking the commons-io Jar for a class which leads me to a wild guess: Could it be a whitespace problem? Is it possible that lib contains a jar with a space in it? Have you tried enclosing the cp option in quotes (i.e. use -cp "lib/*")?
Use quotes.
java -cp "lib/*" net.myapp.Driver /home/myUser.blah.text
Well explained here.
you could also try java -cp /lib/* net.myapp.Driver /home/myUser.blah.text
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 am unable to resolve this issue in which i was trying to executing a java program through bash script .
Why i am getting this error ??
Exception in thread "main" java.lang.NoClassDefFoundError: com/QuoteTester
Caused by: java.lang.ClassNotFoundException: com.QuoteTester
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: com.QuoteTester. Program will exit.
This is my script content quotetest.sh
java -cp ./com/ug_ugbapi-all.jar com.QuoteTester $1
I am executing the script , this way
./quotetest.sh GOOG
From the directory from where i am executing this script quotetest.sh , in that i got a directory by name com under which i got ug_ugbapi-all.jar and QuoteTester.class classes .
Tried every option (placing all the files in same folder com , but nothing worked )
Could anybody please tell me how to resolve it ??
With your given classpath, Java is only going to look in the jar file. For it to find the class file outside of the jar, you have to modify your -cp argument:
java -cp ./com/ug_ugbapi-all.jar:. com.QuoteTester $1
You are getting this error because the com/QuoteTester class cannot be found, which means that the com/QuoteTester class is not on the classpath.
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
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