所以我对通过命令行运行代码完全陌生(我以前习惯使用Eclipse)。当我使用不同的包时,我无法找到编译类的解决方案。
这是我的第一堂课(GreetingsUniverse.java):
package com.ocajexam.tutorial;
import com.ocajexam.tutorial.*;
public class GreetingsUniverse {
public static void main(String[] args) {
System.out.println("Greetings, Universe!");
Earth e = new Earth();
}
}
第二课(Earth.java):
package com.ocajexam.tutorial.planets;
public class Earth {
public Earth() {
System.out.println("Hello from Earth!");
}
}
我尝试按如下方式拼凑它:
javac -d . Earth.java
在com / ocajexam / tutorial / planets中创建地球类文件。
然后,当我尝试:
javac -d . GreetingsUniverse.java
我收到以下错误消息:
GreetingsUniverse.java:9: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
GreetingsUniverse.java:9: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
2 errors
我也试过抛弃-d和其他东西,但一直得到同样的错误。我认为它与不同的包有关。有人可以帮助我吗?
答案 0 :(得分:3)
类Earth
位于com.ocajexam.tutorial.planets
包中,但导入GreetingsUniverse
时导入com.ocajexam.tutorial.*
,这不会导入类Earth
您。因此,将import com.ocajexam.tutorial.*;
更改为import com.ocajexam.tutorial.planets.*;
或import com.ocajexam.tutorial.planets.Earth;