user506710
user506710

Reputation:

java.lang.NoClassDefFoundError in cmd

I am facing this problem while trying to run my java file by writing java filename ....

I have read on many pages the possible ways this could be corrected but unfortunately I have been unable to correct my problem...

  1. First of all I looked at my environment variables and observed that there was no CLASSPATH set and I had pointed PATH correctly to my jre as well as jdk bin in C:\

  2. Second I am able to run javac filename.java and observe that .class file gets built in the local directory.

  3. While writing javac -classpath . filename works writing java -classpath . filename (without .class) results in the same error.

I just don't know how to run my program in command prompt!!!! Please do not give me links to the pages which have given the same answers that I have mentioned above as they do not work in my case.....

Please help....

Upvotes: 1

Views: 3784

Answers (3)

Greg Giacovelli
Greg Giacovelli

Reputation: 10184

Stating javac filename.java and java filename are not enough for this really.

We also need to know the contents of the filename.java or at least the first few lines.

This is the expected way to declare a class

 public class Filename {
    }

This is the way it would have to be to work given your example: java filename

 public class filename {
    }

This is could exist too, but you are probably just messing with us :)

public class SomethingElse {
}

Overall no matter what the filename is on the filesystem, the class has a name and that is the name that the java command is expecting. I would recommend using the upper case first letter form as it is clearer imo

Upvotes: 0

Paŭlo Ebermann
Paŭlo Ebermann

Reputation: 74750

There is a bit little information in your post ... as said, a complete example would really help solving this. Some ideas:

  • Is class named filename? If not, you should make sure to call the right class name in the java command.
  • if you have strange characters in your class name (basically anything apart from A-Z, a-z, 0-9, _), it could be mangled by your file system and thus lead to java not being able to find it. Make sure that the class name is the same as the file name.
  • If you use packages, make sure the package names are congruent to your file structure (see latest questions with tag package for some examples).
  • Is your class public? If not, make it so. (This should give another error, though.)

Upvotes: 0

aioobe
aioobe

Reputation: 420951

Note that if your class resides in some package mypackage, you need to make sure the class file is inside mypackage/ and do

java -classpath . mypackage.YourClass

Upvotes: 3

Related Questions