因此,我对通过命令行运行代码完全陌生(我曾经使用过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中创建一个Earth类文件。
然后,当我尝试:
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和其他内容,但仍然遇到相同的错误。我认为这与不同的软件包有关。有人可以帮帮我吗?
最佳答案
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;
关于java - Java : cannot find symbol when compiling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033418/