aspose file tools*
The moose likes Beginning Java and the fly likes Cannot Find Class File.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot Find Class File...." Watch "Cannot Find Class File...." New topic
Author

Cannot Find Class File....

Rekha Anand
Ranch Hand

Joined: Feb 23, 2008
Posts: 36
Dear Forum Members....

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?

Please help me...
Krep Lock
Ranch Hand

Joined: Sep 19, 2006
Posts: 43
i pretty new, but here's a couple things to try:

1) javac -cp . MyClass.java

2) java MyClass

please note that you do not use java MyClass.class
Jon Parise
Ranch Hand

Joined: Jul 03, 2007
Posts: 81
You need to browse to the folder that contains the .java files to compile them. I am not sure if you are doing that.

cd C:\SomeFolder\
javac SomeFile.java
java SomeFile

Also, does one of the files contain a declaration for:

public static void main(String args[])

Without that function it won't compile.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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...

    C:\>cd jdk1.3\bin\samples

    This will set the current directory to "samples," and the prompt will look like this...

    C:\jdk1.3\bin\samples>


    COMPILING

    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...

    C:\jdk1.3\bin\samples>javac MyFile.java

    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.


    RUNNING

    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...

    C:\jdk1.3\bin\samples>java MyFile

    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
    Rekha Anand
    Ranch Hand

    Joined: Feb 23, 2008
    Posts: 36
    Thanks a lot Marc,

    it is running fine with -cp.

    Regards,
    Rekha
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Cannot Find Class File....