Exception in thread "main" java.lang.NoClassDefFoun - java

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

Related

Unable to the Linux Script for executing a Java Program

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.

Compiling Simple MongoDB + Java Example

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.

Unable to run a Java Program with the command line option -DsocksProxyHost

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

How do I add a .jar file to the classpath on Mac OS X?

I'm starting to become pretty irritated, because EVERY time I try to add a JAR file to the classpath to run a program (which compiles fine in Eclipse) I get a "java.lang.NoClassDefFoundError" exception.
The file swt.jar and GUI.class (main class) are in the same directory. If I issue this command: "java -cp swt.jar GUI", I get this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: GUI Caused
by: java.lang.ClassNotFoundException: GUI 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)
If I formulate the command like this: "java GUI -cp swt.jar", it'll throw this exception:
Exception in thread "main" java.lang.NoClassDefFoundError:
org/eclipse/swt/widgets/Display at GUI.main(GUI.java:9) Caused by:
java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Display 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) ... 1 more
I just want to know how I can add .jar files to the runtime classpath on mac.
All of these commands are being issued from the Terminal on a 64-bit MacBook running Mac OS X Lion.
The problem, I think, is that you are specifying swt.jar as the classpath, but the file you're using is not in the jar. You neeed to specify both the jar and this dir as the classpath.
e.g.
java -cp swt.jar:./ GUI
This one will work, with modifications:
If I formulate the command like this: "java GUI -cp swt.jar"
You just didn't go far enough. You have to add ALL the 3rd party JARs, including the ones for Eclipse SWT and all your other dependencies.

Manual setting CLASSPATH with -cp or -classpath does not work as expected

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

Categories

Resources