• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Cannot Find Class File....

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
     
    Rekha Anand
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lot Marc,

    it is running fine with -cp.

    Regards,
    Rekha
    reply
      Bookmark Topic Watch Topic
    • New Topic