File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sun certified programmer book - chapter 10 question 12 - clarification Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sun certified programmer book - chapter 10 question 12 - clarification " Watch "Sun certified programmer book - chapter 10 question 12 - clarification " New topic
Author

Sun certified programmer book - chapter 10 question 12 - clarification

Abinesh Rajagopal
Greenhorn

Joined: Jul 17, 2011
Posts: 2
Hello, I saw this question has been asked couple of times here and I could not figure the correct reason for the answer given in the book.

Question:
Given following directory structure:


And given the contents of GoDeep.java and Foo.java:

And MyJar.jar contains the following entry:
myApp/Foo.class
If the current directory is x, which commands will successfully execute GoDeep.class and
produce the output 8? (Choose all that apply.)
A. java GoDeep
B. java -classpath . GoDeep
C. java -classpath test/MyJar.jar GoDeep
D. java GoDeep -classpath test/MyJar.jar
E. java GoDeep -classpath test/MyJar.jar:.
F. java -classpath .:test/MyJar.jar GoDeep
G. java -classpath test/MyJar.jar:. GoDeep

Answer is: F and G
Why is c not a valid option?

The jar file is present under the directory test, how will the java command work if current directory is specified in classpath, since the jar file is not in the current directory?
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
Welcome to JavaRanch

Take a look at this http://www.coderanch.com/t/491314/java-programmer-SCJP/certification/Chapter-Quiz#2210517
Abinesh Rajagopal
Greenhorn

Joined: Jul 17, 2011
Posts: 2
Thanks for pointing to the previous question.

I have a question on the answer posted, do we need to include the current directory in class path for executing a class that is in the current directory?
Because GoDeep.class is present under directory "x" and we are executing java command from "x", do we still need (.) in the class path in order to recognize GoDeep?

Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
The most effective way to learn about classpaths is to try it out on your system.
Remove the CLASSPATH environment variable and then use the java command to run a class in the current directory without specifying a classpath.
Michael Schubert
Greenhorn

Joined: Jul 17, 2011
Posts: 5
Abinesh Rajagopal wrote:do we need to include the current directory in class path for executing a class that is in the current directory?
Because GoDeep.class is present under directory "x" and we are executing java command from "x", do we still need (.) in the class path in order to recognize GoDeep?


The short answer is yes.
Both javac and java look for classes in the classpath.
javac also needs a .java file, which it can find in the current directory.
java on the other hand only works with compiled classes (GoDeep is a compiled .class, which needs to be retrievable via the classpath).
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71
trying this exercise with F and G is giving me this error:
Error: Could not find or load main class GoDeep.

anyone? how did you guys run this?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3720
    
  16
Are you running on Windows ? If so you will need to change the : in the classpath value to ;
: only works on *nix


Joanne
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71
Joanne Neal wrote:Are you running on Windows ? If so you will need to change the : in the classpath value to ;
: only works on *nix


Can't thank you enough, i was on ubuntu linux for almost a week and forgot all about the : vs ;
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sun certified programmer book - chapter 10 question 12 - clarification