1) I am still having problem compiling the .java files not residing in the Bin directory. The compiler is not able to find any .java file that is outside the bin directory. I have JVM installed in C:\jdk1.3\bin> and I want to keep all my .java files in C:\jdk1.3\bin\samples> Could you please tell me what exactly should I mention in the classpath and path..? Things were working fine few days back. But I am facing the same problem again.
2) Secondly, if I store my .java file in Bin directory, and then run javac, the source code is compiled successfully. I can see the corresponding .class file in the bin directory. But when I run java on the class file, it gives me the following runtime exception...
Exception in thread "main" java.lang.NoClassDefFoundError : [the file name]
When the class file exists, why is it not able to find it?
Originally posted by Rekha Anand: ...Exception in thread "main" java.lang.NoClassDefFoundError : [the file name]...
There are 2 easy things to check first:
Java class names are case sensitive. So if your class is called "MyClass" and you type "myclass," it won't work.
When you run the java command, the class name you supply should not have any extension after it (no ".java" and no ".class").
If you're still having problems, you need to keep in mind that PATH and CLASSPATH are very different things. Here is a step-by-step...
CHANGING THE DIRECTORY
As Jon pointed out, it's important to set the current directory to where your files are located.
Use the "cd" command to change directories. For example...
This will set the current directory to "samples," and the prompt will look like this...
After changing the directory to where your .java files are saved, you should be able to compile by typing "javac" and the name of the .java file you want to compile. For example...
If you get an error that javac is not a recognized command, that means your PATH variable is not updated correctly. See step 4 of the installation instructions.
After compiling, your .class files should be in the same directory as your .java source file. So from the same prompt, type "java" and the name of the class without any extension...
Remember, Java class names are case sensitive, so "myfile" is not the same as "MyFile."
CLASS NOT FOUND
If you have entered the correct name (case sensitive and without any extension) and you still get a "not found" error, then try specifying the current directory as a CLASSPATH. This is done by inserting -cp, space, dot, space between the java command and the class name...
C:\jdk1.3\bin\samples>java -cp . MyFile
If this works (but does not work without the -cp .), then you have a system CLASSPATH set that does not include a dot for the current directory. See Setting the class path.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org