i am new to java and i just made my first program: HelloWorld and after i compiled it I tried to run it and it gave me this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld Caused by: java.lang.ClassNotFoundException: HelloWorld 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: HelloWorld. Program will exit.
I am using ubuntu version 10.04, I made the program with geany, and compiled it in terminal by using the command javac HelloWorld.java
.
Then i ran it in terminal with the command: java HelloWorld
and then had the error above.
my HelloWorld.java
file looks like this:
public class HelloWorld {
public static void main (String[] args) {
System.out.开发者_开发百科println("Hello World");
}
}
then my HelloWorld.class
file looks like this:
public static void main(string[] args)
You either need to run your java command directly from the directory that contains your .class file, or you need define the classpath, that is, the place where the JVM can find your compiled Java class. Try:
java -classpath <directory> HelloWorld
where <directory>
is the (relative) path to the directory containing your HelloWorld.class file.
Check what JVM is installed on your system. I had same issue with OpenJDK. After removing it and installing Oracle JDK the issue has gone. You can also specify correct JDK path in Project > Properties > Build > Execute Commands. I.g., $JAVA_HOME/bin/java "%e" for Execute command.
When you are running your program, looks like you may be typing:
java HelloWorld.class
instead of
java HelloWorld
When I tried it, it worked like it was supposed to, except when I included ".class", which caused it to give the error you pasted.
Based on your response to Jeen Broekstra's solution I would suggest looking through your code for "slanty" quotations - See here.
Then try compiling: javac HelloWorld.class
and running: java -classpath '/home/(user)/Desktop/java1' HelloWorld
again.
I've just had the same problem in Geany. I realised I was clicking on the "Build" option (on my toolbar it looks like a brick) instead of "Compile" (blue polyhedron converted to red sphere).
精彩评论