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.

coqer
  • 307
  • 1
  • 9

1 Answers1

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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. – coqer Mar 11 '12 at 14:17
  • @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. – Jon Skeet Mar 11 '12 at 14:38
  • ,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! – coqer Mar 12 '12 at 04:05
  • @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` – Jon Skeet Mar 12 '12 at 06:20