Results 1 to 9 of 9
  1. #1
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default Compiling with -d -cp switches

    I have just started studying for the Oracle SCJA, and have hit a snag in the first chapter! Can someone please help me?

    Here is the question: #1433453 - Pastie

    I have the three planets class files (.java files) stored under \com\scjaexam\tutorial\planets.

    My main class code is:

    Java Code:
    package com.scjaexam.tutorial;
    
    public class Exercise103 {
       
       public static void main(String[] args) {
          System.out.println("Greetings, Universe!");
          
          Earth e = new Earth();
          Venus v = new Venus();
          Mars m = new Mars();
       }
    }

    I am trying the following compiler command:

    javac -d . -cp \com\scjaexam\tutorial\planets Exercise103.java

    This produces the following errors:

    Exercise103.java:8: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.Exercise103
    Earth e = new Earth();
    ^
    Exercise103.java:8: cannot find symbol
    symbol : class Earth
    location: class com.scjaexam.tutorial.Exercise103
    Earth e = new Earth();
    ^
    Exercise103.java:9: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.Exercise103
    Venus v = new Venus();
    ^
    Exercise103.java:9: cannot find symbol
    symbol : class Venus
    location: class com.scjaexam.tutorial.Exercise103
    Venus v = new Venus();
    ^
    Exercise103.java:10: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.Exercise103
    Mars m = new Mars();
    ^
    Exercise103.java:10: cannot find symbol
    symbol : class Mars
    location: class com.scjaexam.tutorial.Exercise103
    Mars m = new Mars();
    ^
    6 errors

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default

    What's the package statement at the top of the three files Earth.java, Venus.java, Mars.java?

    db

  3. #3
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by DarrylBurke View Post
    What's the package statement at the top of the three files Earth.java, Venus.java, Mars.java?

    db
    package com.scjaexam.tutorial.planets;

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Did you do step 2? ("java -cp . com.scjaexam.tutorial.GreetingsUniverse")

    Notice how the classpath is specified as "." The Java runtime (or compiler) will try and find classes and packages relative to this classpath. Or, put another way, the classpath represents the top of the package tree. So GreetingsUniverse whose full name is com.scjaexam.tutorial.GreetingsUniverse will be located by starting at . then going to com, then scjaexam then tutorial and finally finding the file GreetingsUniverse.class which is the file containing the class with the appropriate main() method.

    Depending on your circumstances the correct classpath may or may not be "." - but the logic should be the same. You have to specify the location of the top of your class tree: ie the directory (folder) containing the com folder. The Eath class (full name com.scjaexam.tutorial.planets.Earth) will then be located the same way, following the folders until it finds Earth.class within the planets folder.

    Notice that your GreetingsUniverse.java code will have to import the classes from the planets package. (a point not explicitly made in the instructions. but, as noted, it is a separate package so the import has to be made explicitly)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Reviewing those instructions I have no idea why the main() method is inside the Excercide103 class, rather than (a modified) GreetingsUniverse but this doesn't materially alter things.

  6. #6
    codeAJ's Avatar
    codeAJ is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks for your help pbrockway2.

    I added the following code to Exercise103.java (my renamed GreetingsUniverse.java):

    Java Code:
    import com.scjaexam.tutorial.planets.Earth;
    import com.scjaexam.tutorial.planets.Venus;
    import com.scjaexam.tutorial.planets.Mars;
    Compiled ok with: javac -d . -cp . Exercise103.java
    Interprets ok with: java -cp . com.scjaexam.tutorial.Exercise103

    Very confusing topic (for me!)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome. I'm glad it's OK compiling/running OK now.

    It can be somewhat confusing at first - especially if you're more use to working within an IDE.

  8. #8
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You can probably use a wildcard package statement to save some typing:

    import com.scjaexam.tutorial.planets.*;

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can probably use a wildcard package statement to save some typing
    That's a valid option. But code is more self documenting if all and only the classes used from other packages are explicitly imported.

Similar Threads

  1. how to get compiling
    By philgrek in forum New To Java
    Replies: 1
    Last Post: 05-25-2010, 11:53 PM
  2. Using switches...
    By besweeet in forum New To Java
    Replies: 11
    Last Post: 03-04-2010, 04:48 PM
  3. help with switches
    By spots of fire in forum New To Java
    Replies: 1
    Last Post: 01-23-2010, 09:02 PM
  4. how to convert from switches to methods
    By peacehope in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 12:33 AM
  5. Help with switches
    By Daniel in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •