Compiling and running multiple packages using the

2019-07-20 03:37发布

I have been using an IDE but now, in order to prepare my 1Z0-803 exam, I need to run and compile from the command line. The problem is that I have multiple packages and I have tried to find the answer but nothing has worked so far.

So I have :

package com.oca.tutorial;

import com.oca.tutorial.planets.Earth;
import com.oca.tutorial.planets.Mars;
import com.oca.tutorial.planets.Venus;

public class GreetingUniverse {


    public static void main(String[] args) {

        System.out.println("greetings universe");

        new Earth();
        new Mars();
        new Venus();
        }
}

Venus class:

package com.oca.tutorial.planets;

public class Venus {

    public Venus() {

        System.out.println("Hello from Venus");

    }

}

Mars class

 package com.oca.tutorial.planets;

    public class Mars {


        public Mars (){

            System.out.println("Hello from Mars");


        }

    }

And my Earth class

 package com.oca.tutorial.planets;

    public class Earth {


        public Earth (){

            System.out.println("Hello from earth");


        }

    }

Command Line + error enter image description here

Expected output:

greetings universe
Hello from earth
Hello from Mars
Hello from Venus

Fiel Structure for planets :

C:\OCA\com\oca\tutorial\planets

Fiel Structure for main GreetingUniverse :

C:\OCA\GreetingUniverse

error message from command prompt:

enter image description here

3条回答
对你真心纯属浪费
2楼-- · 2019-07-20 04:20

Note that you declare packages for each of your classes. GreetingUniverse is in the com.oca.tutorial package and Venus, Mars, and Earth are in the com.oca.tutorial.planets package. Java requires that a .java file is located in a directory which mirrors its package name. For example, GreetingUniverse.java needs to be in a subdirectory called com\oca\tutorial. This subdirectory can be in the OCA directory which you are currently using to compile from.

If you still get similar errors after moving your .java files to the correct directories, try including all the .java files for every class in the same command-line. It has been a long time since I compiled large-ish projects from the command-line. From what I remember this shouldn't be necessary, but it is certainly worth a try.

Edit:

To clarify, this is how your directory structure should be set up:

C:
+-- OCA
+-- com
+-- oca
+-- tutorial
-- GreetingUniverse.java
+-- planets
-- Earth.java
-- Mars.java
-- Venus.java

Now run the following command:

C:\OCA> javac com/oca/tutorial/GreetingUniverse.java

查看更多
Melony?
3楼-- · 2019-07-20 04:21

First you can cd to the base directory, where com/ is located. Then,

javac -d bin com\oca\tutorial\*.java com\oca\tutorial\planets\*.java

Edit: As the above posters say (and discovered), your file structure is wrong. If you have class GreetingUniverse in package com.oca.tutorial, then file path should be com\oca\tutorial\GreetingUniverse.java.

查看更多
别忘想泡老子
4楼-- · 2019-07-20 04:33

Make sure that all files can be found by the compiler. Go to the directory containing the com folder and use:

javac -d classes com\oca\tutorial\GreetingUniverse.java

or simply

javac -d classes com\oca\tutorial\*.java

with this file structure

com
 |-oca
    |-tutorial
       |  GreetingUniverse.java
       |-planets
          Earth.java
          Mars.Java
          Venus.java

You need to be located in C:\OCA when doing the compilation.

查看更多
登录 后发表回答