Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling and running multiple packages using the command line in Java

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

like image 241
AchillesVan Avatar asked Nov 18 '25 00:11

AchillesVan


1 Answers

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.

like image 180
Reimeus Avatar answered Nov 19 '25 13:11

Reimeus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!