Reputation: 674
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?
Upvotes: 1
Views: 468
Reputation: 1846
You're trying to create an instance of the Earth object, however that is in a seperate package meaning it can't find it. You need to import the Earth class in your GreetingsUniverse class using:
import com.scjaexam.tutorial.planets.Earth;
Upvotes: 1
Reputation: 7521
You need to import Earth
:
package com.scjaexam.tutorial;
import com.scjaexam.tutorial.planets.Earth;
public class GreetingsUniverse {
public static void main(String[] args) {
System.out.println("Greetings, Universe!");
Earth e = new Earth();
}
}
When the compiler says "cannot find symbol: class Earth"
, it is referring to the class you did not import. Be sure to include all packages you use in your class before your class declaration.
Upvotes: 1
Reputation: 1500525
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;
Upvotes: 2