• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ??
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to create MyJar.jar in test directory to see if answer A works.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you set the environment variable to where your jdk is installed?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic