This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Do we need to provide classpath for jar file with java command if jar file is in current directory? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Do we need to provide classpath for jar file with java command if jar file is in current directory?" Watch "Do we need to provide classpath for jar file with java command if jar file is in current directory?" New topic
Author

Do we need to provide classpath for jar file with java command if jar file is in current directory?

Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Hi,

I consider the example from SCJP Sun Certified Programmer for Java 6 Study Guide by Katherine Sierra and Bert Bates (K&B) Page number 817, Chapter 10 and Question no 11. The correct answer according to the book is option A.

The second command is java GetJar, my quesion is how does it find the MyJar.jar without providing classpath or does it use the current directory as used for GetJar.class but it does not work.

11. Given the following directory structure:
test-|
       |- GetJar.java
       |
       |- myApp-|
                      |-Foo.java
And given the contents of GetJar.java and Foo.java:
3. public class GetJar {
4. public static void main(String[] args) {
5. System.out.println(myApp.Foo.d);
6. }
7. }

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.)

Answer:
A.
javac -classpath MyJar.jar GetJar.java
java GetJar


Thank you.


Matloob
OCPJP 1.6
OCPWCD in progress.....
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

This is an error in the question. You indeed have the provide the jar in a classpath. See this topic for more detail.
Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Thanks Jack.................
 
jQuery in Action, 2nd edition
 
subject: Do we need to provide classpath for jar file with java command if jar file is in current directory?
 
Similar Threads
Confusion over java classpath
K&B doubt regarding question in Development chapter
Doubt at K&b chapter 10,question 11
Question related to self test question in SCJP (Sierra/Bates) Java 6 book
Mistake in GuideBook for SCJP Java 6.0, Chapter 10, Self Test question 11