user6311523
user6311523

Reputation:

Compiler does not identify class on the same package

I had some java classes a couple years ago, due to lack of use it all faded away. now ive been getting back into it. i was following a very simple tutorial building something using sublime (it was so simple i didn't bother on getting a proper IDE) the two files are:

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");
    }
}

and

package hello;

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

Greeter compiles dine but HelloWorld gives me the following error: 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

and if i add "import hello.Greeter;" i get:

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

On the IDE it runs fine, can anyone be so kind as to explain whats happening

Upvotes: 1

Views: 2873

Answers (1)

user207421
user207421

Reputation: 311050

javac HelloWorld.java

The problem is here. You're in the wrong directory. You should be up one level, where the package hierarchy starts, and you should be issuing

javac hello/HelloWorld.java

Upvotes: 9

Related Questions