3

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

6
  • Your question ends with "Command Line + error", but you do not say what you tried and what the error is. Can you add this? Commented May 30, 2013 at 16:56
  • What is the file structure under your OCA directory? Commented May 30, 2013 at 16:56
  • @brettjonesdev Theres a screenshot below that that you probably aren't seeing for some reason Commented May 30, 2013 at 16:56
  • p.s. You can copy and paste from the Windows command prompt by clicking on the icon in the upper left corner. This is preferrable to a screenshot when asking for help here because you can provide all the errors. Commented May 30, 2013 at 16:57
  • @Steve Ah, gotcha. Stupid filtered internet... Commented May 30, 2013 at 16:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

i will try this. Hold on please
You will need to create a classes folder if it doesnt already exist for the output class files
This is exactly the structure i have in my folder OCA folder
You need to be located in C:\OCA when doing the compilation
1

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.

Comments

1

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

4 Comments

@Rahoul Please remember to upvote any helpful answers and click the checkmark to accept the one that helps you the most. Good luck with your Java!
I just tried to move GreetingUniverse.java into com\oca\tutorial under the same path of com.oca.tutorial.planets but ... please see new screenshot
@Rahoul You need to compile from C:\OCA and provide the relative path to the .java file you want to compile (e.g. javac com/oca/tutorial/GreetingUniverse.java. Don't cd to the subdirectory because javac will then be unable to find the other .java files.
@Rahoul What does "Not working mean"? I'm glad to continue to help you if you provide more information about your most recent attempt.

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.