Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author
Posts: 587
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Devaney Marcondes
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 James Liguori
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 James Liguori
Author
Posts: 587
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Robert for your support.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

 
Greenhorn
Posts: 20
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for this, - I spent over 3 hours trying to get the javac to work from the command line last night. Including scouring Sourceforge & other sites for how to on Windows cmd compiler arguments.

Glad to know I'm not a total idiot.
IFinally gave up as I always use an IDE anyway, I was just trying to get familiar with the cmd compiler. hopefully that question wont arise on my exam!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andy Ball wrote:IFinally gave up as I always use an IDE anyway, I was just trying to get familiar with the cmd compiler. hopefully that question wont arise on my exam!


If you prepare for OCAJP7 (or OCPJP7) you should preferrably not use an IDE. Reason is fairly simple: an IDE spots every issue in the code for you, but on the actual exam you are completely on your own. No compiler to rely on...
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic