I try to compile a simple Java program with JOGL OpenGL, from the command line. In the Eclipse all works fine, when I compile it from the command line with javac I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
...
Could not find the main class: SimpleScene. Program will exit.
I already add to -classpath all the .jar files I find in JOGL directory. The Makefile to build the program is the following:
SimpleScene:
javac -Xlint:deprecation -classpath \
$(jogl)/joal.jar:\
$(jogl)/jogl.os.x11.jar:\
...
$(jogl)/nativewindow.awt.jar:\
$(jogl)/jogl_cg-natives-linux-i586.jar:. \
SimpleScene.java
I had a similar problem and figured out my simple mistake. GLEventListenere is an interface; not a class.
http://download.java.net/media/jogl/builds/archive/jsr-231-beta5/javadoc_public/javax/media/opengl/GLEventListener.html
I fixed my problem after realizing this because my code that uses GLEventListener should use the keyword 'implements' instead of 'extends'.
Related
Almost everywhere in INTERNET there are these basic steps:
• Compile java files as usual
javac <source-files>
• “Instrument” the byte code
java -jar jcov.jar Instr <application classes>
• Run the code
java -classpath ...:jcov_file_saver.jar ...
• Create a report
java -jar jcov.jar RepGen <jcov xml file> demo
I was able to instrument class files and jar files both, but couldn't run jar one.
Faced this error:
$ java -cp .:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/tdk/jcov/runtime/Collect
at BubbleSort.main(BubbleSort.java:49)
Caused by: java.lang.ClassNotFoundException: com.sun.tdk.jcov.runtime.Collect
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
)
Can anyone please help me out or direct me to some web-page where I can understand this?
You also need to provide the jcov.jar file as well because it contains the com/sun/tdk/jcov/runtime/Collect class.
To supply the jcov.jar at run time, run this
java -cp .:$JCOV/jcov_file_saver.jar -Xbootclasspath/a:$JCOV/jcov.jar -jar BubbleSort.jar
Using this resolved the issue faced thanks to sumedh's answer which pushed me to understand about various kind of classpaths.
Executing instrumented jar:
java -cp . -Xbootclasspath/a:$JCOV/jcov_file_saver.jar -jar BubbleSort.jar
I have tried several approaches as suggested on this website as well as several others to no avail.
-Running Angstrom embedded Linux, OpenJDK6, Shark VM.
File Structure
All .java and .jar files are stored in directory ./src
jarfiles: j1.jar j2.jar
Source code: Coder.java, WrapperClass.java
Compiling and Running
javac -cp j1.jar:j2.jar Coder.java WrapperClass.java
Successfully compiles, outputs Coder.class, WrapperClass.class, as well as several other files including Coder$1.class through Coder$6.class, as well as Coder$Main_thread.class and Coder$Progress_Bar_Thread.class, any insight onto what all these .class files are for?
Run attempts all terminate in java.lang.ClassNotFoundException.
~/coder/src: java Package.Coder
~/coder/src: java -cp jar1.jar:jar2.jar Package.Coder
~/coder: java ./src/Package.Coder
~/coder: java -cp jar1.jar:jar2.jar ./src/Package.Coder
Thank you very much for your help. I have run this on NetBeans and can verify that it's functional code, just a matter of getting it to run on Linux.
Stacktrace is:
Exception in thread "main" java.lang.NoClassDefFoundError: Package/Coder Caused by: java.lang.ClassNotFoundException: Package.Coder
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: Package.Coder. Program will exit
Assuming this structure
/coder
/src
/Package
/Coder.java
/WrapperClass.java
/j1.jar
/j2.jar
You will need to compile from /src as
javac -cp j1.jar:j2.jar Package/Coder.java Package/WrapperClass.java
This will create .class files in /Package. You can then run it, again from /src as
java -cp j1.jar:j2.jar Package.Coder
assuming the Coder class has a main method which is your entry point.
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.
I'm very new to Java world. I want to add the Json lib to compile my program. I downloaded the file from here.
When I tried to compile the program with
javac -classpath json.jar MyClassName.java
I'm getting the error :
Note: JsonSimpleExample.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
And when I tried to run :
java MyClassName
I'm getting:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/JSONObject
at MyClassName.main(MyClassName.java:9)
Caused by: java.lang.ClassNotFoundException: org.json.simple.JSONObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
Where I'm making the mistake?
You have to use the -classpath when running with java as well.
java -classpath json.jar MyClassName
You're making two mistakes:
The message you get from the compiler is a note, and not a compilation error. Follow the instructions to get details
You forget to set the classpath when running the app:
java -classpath json.jar;. MyClassName
(assuming you're on windows, and you're in the directory containing MyClassName.class). If you're on Unix, replace the ; by a :.
Note that it's bad practice to put classes in the default, root package.
Reason to the error was ,the class was present during compile time and let's application to compile successfully and linked successfully but not available during run-time.
java -classpath json.jar MyClassName (or)
java -cp json.jar MyClassName
The first output is warning from the Java compiler about use of unchecked code (like raw collections). The class file would have been successfully generated.
If you want to fix these warnings, then you can compile with -Xlint command line switch and fix/suppress the unchecked/unsafe warnings.
I have been trying for what seems like two days now to get my java application to compile from the command line in Ubuntu. I know I have Java installed because I can run my applications in Eclipse & Netbeans and they work fine. But if I want to compile my applications from the command line I get the following error message:
javac Main.java
Everythings fine, no errors or anything. Then I try:
java Main
And I get this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: input/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
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)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: Main. Program will exit.
Try:
java input.Main
By the looks of your error, your Main class is in package "input". You need to specify package name when running a class, not the filename.
Open terminal and paste this command:
export CLASSPATH=.:/usr/local/tomcat/common/lib/jsp-api.jar:/usr/local/tomcat/common/lib/servlet-api.jar:/home/trenog/javokapi/bin/xmlrpc.jar
This looks like a classic Classpath problem. Eclipse and Netbeans will set up the classpath for you, but when you're writing to the command line, you're on your own.
Assuming you're using BASH, try typing the following into the command line:
CLASSPATH=/path/to/your/java/class/file
Or, alternately, you can do this from the java command line:
java -cp /path/to/your/java/class/file Main
Follow this link for more info.
EDIT: Well, I see you figured it out. Congrats.
The classloader simply can't find the class input.Main.
The class should be located in the directory ./input, the file inside that directory should be called Main.class and the java command should be 'java input.Main'.