关于java包

来源:百度知道 编辑:UC知道 时间:2024/05/10 17:53:05
import parent.child.Location;
class ParentTest {
protected ParentTest() {
}
public static void main(String[] args) {
Location objLoc = new Location();
objLoc.disp();
}
}
……………………………………………………………………

package parent.child;
public class Location {
public final void disp() {
System.out.println("child子包中的Location类 ");
}
}
第一个是调用包,第二个是包。
我用javac -d .ParentTest不能运行。出现如下错误:
ParentTest.java:9: package parent.child does not exist
import parent.child.Location;
^
ParentTest.java:29: cannot access Location
bad class file: .\Location.java
file does not contain class Location
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
Location objLoc = new Location();
^
2 errors
请问如何解决????thank you!
我用的是javac -d .ParentT

因为Location 类没有被编译过,所以找不到,你只编译并运行了ParentTest
先javac -d .Location .java再试试

javac -d .ParentTest 应改为 javac -d .ParentTest.java
另外ParentTest类的包你放在哪了?你应该把ParentTest类放在能见到parent.child.Location这个包的目录下,即ParentTest类所在的文件夹应该在parent文件夹的外部。

dfg