Java Using a non-public class from the current package makes error compilation

I have the following two source files

World.java File

package planets;

public class World {
    public static void main(String[] args) {
        Mars.land();
    }
}

Moon.java file

package planets;

public class Moon {
    public static void land() {
        System.out.println("Hello Moon");
    }
}

class Mars {
    public static void land() {
        System.out.println("Hello Mars");
    }
}

As we can see, it Moon.javacontains two classes: an open Moonclass and a non-public class Mars.

The files are inside the directory planets, the directory tree is shown below

+current-dir:
+----+planets:
      +----+World.java
      +----+Moon.java

Now, if I try to compile from the Windows command prompt (I'm inside the folder current-dir) by typing

javac planets\World.java

I get this error message:

planets\World.java:5: error: cannot find symbol
       Mars.land();
       ^
  symbol:   variable Mars
  location: class World
1 error

This is very strange because I know that the compiler is looking for non-public classes in all the source files of the current package. Also Cay Horstmann Core Java Vol 1, 10th ed. pp. 192-193 states that:

[...] . . , , , .

, Eclipse Oxygen, . , Eclipse .

javac ?

EDIT: CLASSPATH. .

+4
1

( "current-dir" )

  • javac\Moon.java
  • javac -cp. \World.java
  • java -cp. planets.World
0

Source: https://habr.com/ru/post/1682625/


All Articles