编译器不识别同一个包上的类

时间:2017-07-04 11:33:06

标签: java

几年前我有一些java课程,由于缺乏使用,它们都消失了。现在我已经回到了它。我正在按照一个非常简单的教程来构建使用sublime的东西(它很简单,我没有费心去获得一个合适的IDE)这两个文件是:

package hello;

public class HelloWorld {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
        System.out.println("random alt shift");
    }
}

package hello;

public class Greeter {
    public String sayHello() {
        return "Hello world!";
    }
}

Greeter编译用餐但是HelloWorld给了我以下错误: javac HelloWorld.java

HelloWorld.java:6: error: cannot find symbol
        Greeter greeter = new Greeter();
        ^
  symbol:   class Greeter
  location: class HelloWorld
HelloWorld.java:6: error: cannot find symbol
        Greeter greeter = new Greeter();
                              ^
  symbol:   class Greeter
  location: class HelloWorld
2 errors

如果我添加“import hello.Greeter;”我明白了:

HelloWorld.java:2: error: cannot find symbol
import hello.Greeter;
            ^
  symbol:   class Greeter
  location: package hello
HelloWorld.java:6: error: cannot find symbol
        Greeter greeter = new Greeter();
        ^
  symbol:   class Greeter
  location: class HelloWorld
HelloWorld.java:6: error: cannot find symbol
        Greeter greeter = new Greeter();
                              ^
  symbol:   class Greeter
  location: class HelloWorld
3 errors

在IDE上它运行良好,任何人都可以如此善良地解释发生了什么

1 个答案:

答案 0 :(得分:7)

javac HelloWorld.java

问题出在这里。你在错误的目录中。你应该在一个级别,包层次结构开始,你应该发布

javac hello/HelloWorld.java