1

I can't run the java program in the CMD,but it's OK in the eclipse. enter image description here

These are my configures:

CLASSPATH:.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar

JAVA_HOME:C:\Program Files\Java\jdk1.7.0_03

PATH:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin

I don't think there is something wrong with the program,because I am not successful in running even one program.All these programs come from the official website of Head First Java.I can use javac without any problems.

I can only provide these now.If you need more information.I'll provide it to you.

3
  • Check your classpath inside eclipse vs the global one. Commented Mar 11, 2012 at 13:50
  • possible duplicate of NoClassDefFoundError Commented Mar 11, 2012 at 13:51
  • Is the name of the class with main() BeerSong, not beersong? Commented Mar 11, 2012 at 13:51

1 Answer 1

4

You should be in f:\pro\hfjavafinalsamples and run

java chap01.BeerSong

You need to give the java command the fully qualified class name, which according to the error message is chap01.BeerSong. Then java will look for the class BeerSong within the chap01 package by checking in the chap01 directory.

Note that Java is case-sensitive as well, even though the file system isn't - the class simple name is BeerSong, not beersong.

Sign up to request clarification or add additional context in comments.

4 Comments

I read a few articles about this problem,I think the main problem is the "package".I just start to study the Java,and I have no concept of the "package".when I use the Eclipse,I often use the default package.So,I can't run my own program even by using the style you provided.However,I will try to solve this problem by getting more knowledge of Java.
@user911865: Well there were three problems in your approach: you weren't specifying the package name in the class name, you were in the wrong directory (the chap01 directory instead of the "root" of your logical classpath), and you weren't using the right class name in terms of casing. I don't know why you think you "can't run" your own program using the approach I've given... you certainly will be able to. But yes, you should learn about packages very soon.
,the first line of BeerSong is package chap01;but when I use Eclipse to create the class,I often use the default package,so there is no package declaration.I will try to solve this problem by myself,if I can't ,I'll ask you for help.Thank you!
@user911865: If you'd used the default package in this case, your class files would be in f:\pro\hfjavafinalsamples directly, and you'd just run java BeerSong - because in that case the fully-qualified class name would just be BeerSong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.