I have these programs in java:
//file ../src/com/scjaexam/tutorial/GreetingsUniverse.java
package com.scjaexam.tutorial;
public class GreetingsUniverse {
public static void main(String[] args) {
System.out.println("Greetings, Universe!");
Earth e = new Earth();
}
}
//file ../src/com/scjaexam/tutorial/planets/Earth.java
package com.scjaexam.tutorial.planets;
public class Earth {
public Earth() {
System.out.println("Hello from Earth!");
}
}
I am able to compile with no error the second one using:
javac -d classes src/com/scjaexam/tutorial/planets/Earth.java
This puts the compiled file Earth.class
in the ../classes/com/scjaexam/tutorial/planets/
folder as expected. Now I have to compile the main class GreetingsUniverse.java
but this command fails:
javac -d classes -cp classes src/com/scjaexam/tutorial/GreetingsUniverse.java
src/com/scjaexam/tutorial/GreetingsUniverse.java:7: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
src/com/scjaexam/tutorial/GreetingsUniverse.java:7: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
What is the right command to compile (and then tu run) this program?
You haven't imported the Earth
class, so the compiler doesn't know what Earth
refers to. You should have this line at the start of your GreeingsUniverse.java
file:
import com.scjaexam.tutorial.planets.Earth;
See more on this question at Stackoverflow