Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error in kb 6 book self test question no. 11 , page 817-818 of chapter developement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Error in kb 6 book self test question no. 11 , page 817-818 of chapter developement" Watch "Error in kb 6 book self test question no. 11 , page 817-818 of chapter developement" New topic
Author

Error in kb 6 book self test question no. 11 , page 817-818 of chapter developement

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1


1. Given the following directory structure:
test-|
|- GetJar.java
|
|- myApp-|
|-Foo.java
And given the contents of GetJar.java and Foo.java:

3. package myApp;
4. public class Foo { public static int d = 8; }
If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar
located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
Answer:
✓ A is correct. Given the current directory and where the necessary files are located, these
are the correct command line statements.
B and D are wrong because javac MyJar.jar GetJar.java is incorrect syntax. C is wrong
because the -classpath MyJar.java in the java invocation does not include the test directory.

the book says answer is A. but when i ran the program with proper structure it came out that none of the option is correct. the actual answer is with a little modification on option C as javac -classpath MyJar.jar GetJar.java, java -classpath MyJar.jar:. GetJar. here what i changed was added current directory to classpath. the answer A is not correct because GetJar needs myApp/Foo which is in MyJar.jar but we havent specified the classpath for the jar file MyJar.jar. please confirm the same ??
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Try to create MyJar.jar in test directory to see if answer A works.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

it does not work. i have created the exact scenario given in the book. the jar MyJar.jar is in the test directory itself. no option worked. so i assume no answer correct.
Pondurai Singh
Ranch Hand

Joined: Jun 27, 2011
Posts: 35
Daniel Negut wrote:You probably have the CLASSPATH environment variable defined (and the test folder is not included).

Question 11 assumes that there is no CLASSPATH environment variable defined.


Ref: coderanch.com/t/270643/java-programmer-SCJP/certification/Error-Sun-Certified-Programmer-Java

Software Developer, Oracle Java Certification Training Labs at EPractize Labs.
W Lin
Greenhorn

Joined: Mar 16, 2012
Posts: 11
Before you do your test, rename "CLASSPATH" in your system environment to a different name temporarily.

The reason is, the default classpath is "." if you don't have "CLASSPATH" defined using command line or in system environment. So when the 2nd line of answer (A) Java GetJar is invoked, it automally search current directory.

Let me know if it works for you.



gurpeet singh wrote:
1. Given the following directory structure:
test-|
|- GetJar.java
|
|- myApp-|
|-Foo.java
And given the contents of GetJar.java and Foo.java:

3. package myApp;
4. public class Foo { public static int d = 8; }
If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar
located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
Answer:
✓ A is correct. Given the current directory and where the necessary files are located, these
are the correct command line statements.
B and D are wrong because javac MyJar.jar GetJar.java is incorrect syntax. C is wrong
because the -classpath MyJar.java in the java invocation does not include the test directory.

the book says answer is A. but when i ran the program with proper structure it came out that none of the option is correct. the actual answer is with a little modification on option C as javac -classpath MyJar.jar GetJar.java, java -classpath MyJar.jar:. GetJar. here what i changed was added current directory to classpath. the answer A is not correct because GetJar needs myApp/Foo which is in MyJar.jar but we havent specified the classpath for the jar file MyJar.jar. please confirm the same ??
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Do you set the environment variable to where your jdk is installed?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

guys i double checked it again. i have no classpath set. in the answer given as option A as follows :
javac -classpath MyJar.jar GetJar.java
java GetJar



the first part i.e. compilation is file. error comes when we run GetJar using java GetJar. since we are in test directory so when we do java GetJar , it will be default classpath(i do not have environment variable set). so it will look for GetJar in the current directory(test) which will be found. but GetJar needs Foo class which is in MyJar.jar , which too is in test (current) directory. but it is not able to find that Foo class . so i think we have to specify jar file on the classpath as well as the current directory. i ran program twice using the exact scenario given in the book on two different jvm's but it worked the same and gave me error when i ran GetJar. so the only option is if someone else run it or the answers given are wrong .
W Lin
Greenhorn

Joined: Mar 16, 2012
Posts: 11
I just verified, yes you are correct.

I guess, in this question, when people make the MyJar.jar, they don't remove the compiled Foo.class from myApp folder after making MyJar.jar, thus it makes "java GetJar" work in answer (A)



gurpeet singh wrote:guys i double checked it again. i have no classpath set. in the answer given as option A as follows :
javac -classpath MyJar.jar GetJar.java
java GetJar



the first part i.e. compilation is file. error comes when we run GetJar using java GetJar. since we are in test directory so when we do java GetJar , it will be default classpath(i do not have environment variable set). so it will look for GetJar in the current directory(test) which will be found. but GetJar needs Foo class which is in MyJar.jar , which too is in test (current) directory. but it is not able to find that Foo class . so i think we have to specify jar file on the classpath as well as the current directory. i ran program twice using the exact scenario given in the book on two different jvm's but it worked the same and gave me error when i ran GetJar. so the only option is if someone else run it or the answers given are wrong .
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

W Lin wrote:I just verified, yes you are correct.

I guess, in this question, when people make the MyJar.jar, they don't remove the compiled Foo.class from myApp folder after making MyJar.jar, thus it makes "java GetJar" work in answer (A)



gurpeet singh wrote:guys i double checked it again. i have no classpath set. in the answer given as option A as follows :
javac -classpath MyJar.jar GetJar.java
java GetJar



the first part i.e. compilation is file. error comes when we run GetJar using java GetJar. since we are in test directory so when we do java GetJar , it will be default classpath(i do not have environment variable set). so it will look for GetJar in the current directory(test) which will be found. but GetJar needs Foo class which is in MyJar.jar , which too is in test (current) directory. but it is not able to find that Foo class . so i think we have to specify jar file on the classpath as well as the current directory. i ran program twice using the exact scenario given in the book on two different jvm's but it worked the same and gave me error when i ran GetJar. so the only option is if someone else run it or the answers given are wrong .



yes W lin. i also thought the same, that people might not remove compiled Foo class in the myApps folder because of which they get the result right. or we can also assume that maybe the book question's intention was just that i.e. to search for Foo class in the myApps folder. but if it was that then there was no fun of making MyJar.jar. anyways thanks for confirming the same.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in kb 6 book self test question no. 11 , page 817-818 of chapter developement
 
Similar Threads
Mistake in GuideBook for SCJP Java 6.0, Chapter 10, Self Test question 11
Chapter 10 K&B Self Test Question 11 Help
Confusion over java classpath
Doubt at K&b chapter 10,question 11
Question related to self test question in SCJP (Sierra/Bates) Java 6 book