日期:2014-05-20  浏览次数:20547 次

【大家帮我看一下】 我这个怎么错了?同一包之间 怎么调用别的类的方法?
程序1:
package   abc;
public   class   Client{
public   static   void   main(String[]   args){
Server   ser   =   new   Server();
System.out.println(ser.PI+ser.sayHello);
System.exit(0);

}
}


程序2:

package   abc;
public   class   Server{
final   double   PI   =   3.1415926;
public   static   void   sayHello(){
System.out.println( "hello   baby ");
}
}
想实现通过程序1调用程序2里的方法   打印出 "hello   baby "   和PI的值

我先   F:\java> javac   -d   ke   ke/Server.java
再       F:\java> javac   -d   ke   ke/Client.java
编译第一个没问题  
到了第二个就出错了
F:\java> javac   -d   ke   ke/Client.java
ke/Client.java:4:   找不到符号
符号:   类   Server
位置:   类   abc.Client
                                Server   ser   =   new   Server();
                                ^
ke/Client.java:4:   找不到符号
符号:   类   Server
位置:   类   abc.Client
                                Server   ser   =   new   Server();
                                                                  ^
2   错误

我的CLASSPATH     :   .;F:\java\ke\abc;


------解决方案--------------------
有点乱。

1, your package name is "abc ". If I am not wrong, your "abc " directory is under directory "ke ". You should put your two .java source files under directory "abc ".

2, When you want to compile, you 'd better execute javac in directory "ke ".
F:\java\ke\javac abc\*.java

3, You will see the compiled .class files in directory "abc ".

4, F:\java\ke\java abc.Server or F:\java\ke\java abc.Client to execute programs.

5, Your class path should be .;F:\java\ke (without "abc ", becaue "abc " is a package name).

6, What the hell do you need to specify a CLASSPATH varialbe? I have been using Java for 7 years, but I have never created a CLASSPATH System Variable. Specifying a System CLASSPATH variable may create confusing problems when you have many java programs on the same system. If a CLASSPATH variable is really necessary for an application, I 'd use a batch (.bat) file to set the CLASSPATH variable for this application only. Other Java applications will not be affected.