[java] Cannot compile
I recently got the Java SDK installed and I found a tutorial on the net about creating your first program. I put the code in notepad, saved it as MyProgram.java and ran javac. It created a class file, but when I try to compile that with java it gives me the following error:
Exception in thread "main"java.lang.NoClassDefFoundError: MyProgram
I have no idea what that means. Is it my source code thats causing the problem, or did I mess up when installing the SDK. Heres my source code. I dont understand it all right now, so any help is appreciated.
public class MyProgram {
public static void main(String[] args) {
System.out.println(
"Hello World.");
}
}
If it created a class file then it worked.
Stepps to compiling and running a java file
javac MyProgram.java
-this creates MyProgram.class
java MyProgram
-runs the program notice that the .class is left out
admire your creation
"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!
Stepps to compiling and running a java file
javac MyProgram.java
-this creates MyProgram.class
java MyProgram
-runs the program notice that the .class is left out
admire your creation
"Give a man a fish and he will eat for a day, drown a man in the water and the fish will eat for a week!
If your "CLASSPATH" variable is set to one version of java libraries, and you use java.exe from another version of java, you''ll get that error.
For example, say you have c:\java1.2 and c:\java1.4
And your CLASSPATH is set to c:\java1.2, but you are running: c:\java1.4\bin\java.exe - then you''ll get the error.
You can either set CLASSPATH correctly, or unset it.
For example in Cygwin/unix you do this:
unset CLASSPATH
and in DOS shell you do:
set CLASSPATH=
http://www.anywherenotes.com - never lose another note again.
For example, say you have c:\java1.2 and c:\java1.4
And your CLASSPATH is set to c:\java1.2, but you are running: c:\java1.4\bin\java.exe - then you''ll get the error.
You can either set CLASSPATH correctly, or unset it.
For example in Cygwin/unix you do this:
unset CLASSPATH
and in DOS shell you do:
set CLASSPATH=
http://www.anywherenotes.com - never lose another note again.
Chances are, you were trying to run it by running "java MyClass.class" when instead you need "java MyClass". This is because java searches for the name of the class without regard to the extension... To be honest, I don''t know why Sun bothered making an extension, other than possibly to stay with the whole Windows-likes-extensions thing... Not sure though.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement