program A:
package myinterface.com;
public class Myinterface
{
public int i=20;
public Myinterface()
{ System.out.println("Myinterface is called");
}
}
program B:
import myinterface.com.*;
public class testvar {
public static void main(String []args){
Myinterface obj=new Myinterface();
System.out.println("my god!"+obj.i);
}}
//
jdk echo such exeption;
testvar.java:5: 无法访问 Myinterface
错误的类文件: .\Myinterface.java
文件不包含类 Myinterface
请删除该文件或确保该文件位于正确的类路径子目录中。
Myinterface obj=new Myinterface();
^
1 错误
//
but when "import myinterface.com.*;" is replaced by "import myinterface.com.Myinterface ;" it works well.
why? who can tell me?? thank you .
package myinterface.com;
public class Myinterface
{
public int i=20;
public Myinterface()
{ System.out.println("Myinterface is called");
}
}
program B:
import myinterface.com.*;
public class testvar {
public static void main(String []args){
Myinterface obj=new Myinterface();
System.out.println("my god!"+obj.i);
}}
//
jdk echo such exeption;
testvar.java:5: 无法访问 Myinterface
错误的类文件: .\Myinterface.java
文件不包含类 Myinterface
请删除该文件或确保该文件位于正确的类路径子目录中。
Myinterface obj=new Myinterface();
^
1 错误
//
but when "import myinterface.com.*;" is replaced by "import myinterface.com.Myinterface ;" it works well.
why? who can tell me?? thank you .
you can have a search
java中的包会对应到一个文件夹,楼主提出的两个文件应该是这样一个文件夹结构:(假设在C:根目录下)C:/
|-- test.java (源文件)
|-- TestPack (文件夹)
|-- TestClass.java (源文件)楼主问题的根源在于CLASSPATH的设置会影响javac的执行方式: 当你导入一个包中所有的类(import TestPack.*)时,javac在编译时并不确定你要使用的TestClass类是TestPack包中的类,还是其他包中的类,它会根据你机器上的CLASSPATH环境变量的值去查找类,通常我们在CLASSPATH中会设置一个点(.),表示当前目录,如果没有CLASSPATH环境变量,那么在JDK1.4之后,默认也是查找当前目录。javac在按照文件名(不包括后缀)来查找类,于是找到TestClass.java,而这个文件根本不是字节码文件,当然就报错了。当你导入一个具体的类时,javac在分析源文件时就知道了你程序中用的类是TestPack.TestClass,那么它就在CLASSPATH环境变量下查找TestPack目录下的TestClass类,正好有,于是正确执行。
.class文件在对应的目录下