1

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?

1
  • Your commands are ok, but in the source code it is nowhere mentioned that Earth is com.scjaexam.tutorial.planets.Earth Commented Oct 9, 2013 at 14:01

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

So being in a "subpackage" is the same as being in a different package?
@Red There is no such thing as subpackage in Java. It just happens that your two package names share a common prefix.
1

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;  

2 Comments

So being in a "subpackage" is the same as being in a different package?
Yes. Sub-packages are only used for organising classes.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.