Results 1 to 9 of 9
Thread: Compiling with -d -cp switches
- 06-04-2011, 06:15 AM #1
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
- 06-04-2011, 06:18 AM #2
What's the package statement at the top of the three files Earth.java, Venus.java, Mars.java?
db
- 06-04-2011, 06:28 AM #3
- 06-04-2011, 06:50 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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)
- 06-04-2011, 06:56 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 06-04-2011, 11:51 AM #6
Thanks for your help pbrockway2.
I added the following code to Exercise103.java (my renamed GreetingsUniverse.java):
Compiled ok with: javac -d . -cp . Exercise103.javaJava Code:import com.scjaexam.tutorial.planets.Earth; import com.scjaexam.tutorial.planets.Venus; import com.scjaexam.tutorial.planets.Mars;
Interprets ok with: java -cp . com.scjaexam.tutorial.Exercise103
Very confusing topic (for me!)
- 06-04-2011, 10:31 PM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 06-05-2011, 01:07 AM #8
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
You can probably use a wildcard package statement to save some typing:
import com.scjaexam.tutorial.planets.*;
- 06-05-2011, 03:25 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
how to get compiling
By philgrek in forum New To JavaReplies: 1Last Post: 05-25-2010, 11:53 PM -
Using switches...
By besweeet in forum New To JavaReplies: 11Last Post: 03-04-2010, 04:48 PM -
help with switches
By spots of fire in forum New To JavaReplies: 1Last Post: 01-23-2010, 09:02 PM -
how to convert from switches to methods
By peacehope in forum New To JavaReplies: 4Last Post: 03-27-2009, 12:33 AM -
Help with switches
By Daniel in forum New To JavaReplies: 2Last Post: 07-04-2007, 08:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks