This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Associate Certification (OCAJP 7) and the fly likes Probable error on 1-3 exercise on OCA Java  SE 7 Programmer I Study Guide (Robert Liguori) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7)
Bookmark "Probable error on 1-3 exercise on OCA Java  SE 7 Programmer I Study Guide (Robert Liguori)" Watch "Probable error on 1-3 exercise on OCA Java  SE 7 Programmer I Study Guide (Robert Liguori)" New topic
Author

Probable error on 1-3 exercise on OCA Java SE 7 Programmer I Study Guide (Robert Liguori)

Devaney Marcondes
Greenhorn

Joined: Nov 22, 2012
Posts: 22
Hi.
I was trying to do the 1-3 exercise of the OCA Java SE 7 Programmer I Study Guide (Robert Liguori).
I do the Mars.java, Earth.java and the Venus.java in the <PROJECT>\src\com\ocajexam\tutorial\planets and this compiled as well, but when I tried to compile the GreetingsUniverse.java in the <PROJECT>\src\com\ocajexam\tutorial\ the console showed me the error:
c:\Users\xxxx\Documents\workspace\certificate>javac -d classes -cp .\classes\com\ocajexam\tutorial\planets src\com\ocajexam\tutorial\GreetingsUniverse.java
src\com\ocajexam\tutorial\GreetingsUniverse.java:3: error: package com.ocajexam.tutorial.planets does not exist
import com.ocajexam.tutorial.planets.*;
^
src\com\ocajexam\tutorial\GreetingsUniverse.java:12: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
src\com\ocajexam\tutorial\GreetingsUniverse.java:12: error: cannot find symbol
Earth e = new Earth();
^
symbol: class Earth
location: class GreetingsUniverse
src\com\ocajexam\tutorial\GreetingsUniverse.java:13: error: cannot find symbol
Venus v = new Venus();
^
symbol: class Venus
location: class GreetingsUniverse
src\com\ocajexam\tutorial\GreetingsUniverse.java:13: error: cannot find symbol
Venus v = new Venus();
^
symbol: class Venus
location: class GreetingsUniverse
src\com\ocajexam\tutorial\GreetingsUniverse.java:14: error: cannot find symbol
Mars m = new Mars();
^
symbol: class Mars
location: class GreetingsUniverse
src\com\ocajexam\tutorial\GreetingsUniverse.java:14: error: cannot find symbol
Mars m = new Mars();
^
symbol: class Mars
location: class GreetingsUniverse
7 errors

After that I tried compiling with the -sourcepath option, this way:
c:\Users\XXXXXX\Documents\workspace\certificate>javac -d classes -sourcepath src src\com\ocajexam\tutorial\GreetingsUniverse.java

and this work fine.
My question is, this solution was the solution expected from the book? Because until this point on the book don't have nothing that say about the option -sourcepath


Thanks
Robert Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 493
    
    5

Hello Devaney,

Thanks for the questions

Below is one way to complete the example (note: I didn't use a 'src' folder for this example, and should have)

-- Robert


(Step 0)
Create a 'code' folder and do the following steps relative to being here (e.g., c:\code\).

(Step 1) Create the following:

com\ocajexam\tutorial\GreetingsUniverse.java


(Steps 2,3,4)
Create the following:

com\ocajexam\tutorial\planets\Earth.java, Mars.java and Venus.java

e.g.:


(Step 5)
Create a 'classes' folder (next to the com folder). (e.g., c:\code\classes\)

(Step 6)
Now build:


(Step 7)
Now run:


Output:
Greetings, Universe!
Hello from Earth!
Hello from Venus!
Hello from Mars!


My Blog (I frequently post on Java, JSF and NetBeans)
Devaney Marcondes
Greenhorn

Joined: Nov 22, 2012
Posts: 22
I think I understood the problem. The misunderstand was that in the book is wrote:
    5. Ensure that all of the source code is in the paths src/com/ocajexam/tutorial/and src/com/ocajexam/tutorial/planets/.
And I did it.
When I did as you wrote on the post, the code compiled fine. If I use the src folder, I must use the -sourcepath option.

Thanks.
Robert Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 493
    
    5

Devaney Marcondes wrote:I think I understood the problem. The misunderstand was that in the book is wrote:
    5. Ensure that all of the source code is in the paths src/com/ocajexam/tutorial/and src/com/ocajexam/tutorial/planets/.
And I did it.
When I did as you wrote on the post, the code compiled fine. If I use the src folder, I must use the -sourcepath option.

Thanks.

updating...

Robert Liguori
Author
Ranch Hand

Joined: Nov 27, 2007
Posts: 493
    
    5

Devaney Marcondes wrote:I think I understood the problem. The misunderstand was that in the book is wrote:
    5. Ensure that all of the source code is in the paths src/com/ocajexam/tutorial/and src/com/ocajexam/tutorial/planets/.
And I did it.
When I did as you wrote on the post, the code compiled fine. If I use the src folder, I must use the -sourcepath option.

Thanks.


Thank you Devaney for your observations... from my testing...

You may use -cp or -sourcepath, interchangeably

javac -d classes [ -cp | -sourcepath ] src src\com\ocajexam\tutorial\GreetingsUniverse.java
java -cp classes com.ocajexam.tutorial.GreetingsUniverse

Here's a good link on the differences between 'sourcepath' and 'classpath': http://stackoverflow.com/questions/2441760/differences-between-classpath-and-sourcepath-options-of-javac. We did not see a need to include sourcepath in the book at the time, perhaps we should have... note that 'sourcepath' is not on the exam.

-- Robert
Devaney Marcondes
Greenhorn

Joined: Nov 22, 2012
Posts: 22
Thank you Robert for your support.
Stephan Eisenhoff
Greenhorn

Joined: Aug 09, 2013
Posts: 3
I followed all the steps as laid out by Robert, but I keep getting this error:

javac -d classes com/ocajexam/tutorial/GreetingsUniverse.java
com/ocajexam/tutorial/GreetingsUniverse.java:2: error: cannot access Earth
import com.ocajexam.tutorial.planets.Earth;
^
bad class file: ./com/ocajexam/tutorial/planets/Earth.class
class file contains wrong class: com.sjcaexam.tutorial.planets.Earth
Please remove or make sure it appears in the correct subdirectory of the classpath.

How do I solve this problem?
Stephan Eisenhoff
Greenhorn

Joined: Aug 09, 2013
Posts: 3
Stephan Eisenhoff wrote:I followed all the steps as laid out by Robert, but I keep getting this error:

javac -d classes com/ocajexam/tutorial/GreetingsUniverse.java
com/ocajexam/tutorial/GreetingsUniverse.java:2: error: cannot access Earth
import com.ocajexam.tutorial.planets.Earth;
^
bad class file: ./com/ocajexam/tutorial/planets/Earth.class
class file contains wrong class: com.sjcaexam.tutorial.planets.Earth
Please remove or make sure it appears in the correct subdirectory of the classpath.

How do I solve this problem?


Okay it seems that I solved the problem by making a new code folder and doing everything again. I still do not know what this error means, but maybe I had some earlier .class files that were corrupt?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Probable error on 1-3 exercise on OCA Java SE 7 Programmer I Study Guide (Robert Liguori)
 
Similar Threads
<indentifier> expected
Cant compile the struts application withEclispse 3.4
problem in compiling servlet
Can Import create a directory "planets"
compiling packages with javac using appropriate directory structure