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 Beginning Java and the fly likes Confusion over java classpath 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 » Java » Beginning Java
Bookmark "Confusion over java classpath" Watch "Confusion over java classpath" New topic
Author

Confusion over java classpath

C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
Hi,

I have a confusion regarding one of the statements written in K.Sierra's book:


For any given class the java virtual machine will need to find exactly the same supporting classes tat the javac compiler needed to find at compilation time.

Then there is a question in the Exercise as below:


Question
====================================================
Given the following directory structure:

test\GetJar.java
test\myApp\Foo.java

Below are the contents of two java files:

public class GetJar {

public static void main(String[] args) {


System.out.println(myApp.Foo.d);




}


}
=================================================================
package myApp;

public class Foo {

public static int d=8;

}



If the current directory is test ,which set(s) of commands will succesfully compile the two .java files and produce the output 8? (Choose all that apply.)

I am providing the correct answer as per the book:

[Correct Answer]
javac myApp/Foo.java
//put into test a jar file
[/Correct Answer]










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



Please let me know how is A correct.

As per the book's statement both javac and java tatements for Getjar should include the option -classpath statements.

Regards,
Chandan










C Kushtawar
Ranch Hand

Joined: Jan 20, 2009
Posts: 37
I mean to ask:

If the javac statement required the jar file then why not the java statement?
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

I have a confusion regarding one of the statements written in K.Sierra's book


I am confused on seeing the multiple "same" question. please use code tags. This is nothing to do with "Threads" please post in Java beginner forum or any of the Bartender can move for you
Vishals Saxena
Greenhorn

Joined: Feb 02, 2009
Posts: 4
Yes It made me confused too ....that

JVM needs to find the same classes as the javac( java compiler ) needs to find!!!

But now its more clear to me and its true!!!

you just mugup this statement its really true and
as of your qustion

see here Getjar class is using the static variable of the Foo class
and Foo class is in myApp package........as you must have read in K.Sierra's book
that when we create a jar file the directory structure is preserved in that file!!!

so here as the javac ( java compiler ) needs to find Foo class either class paths will work
the one given in option a of the book i.e
a) javac -cp MyJar.jar GetJar.java

as well as
javac -cp . GetJar.java or javac Getjar.java
( because as you can see in question the System.out.println statement is using fully qualified class name that is myApp.Foo so the javac will search in current directory that is test and will find myApp/Foo.java also javac command searches for the class in current directory by default )

as for the java command
if you say
java -cp MyJar.jar GetJar
it wont find GetJar.class as it is not in the MyJar.jar file........phew!!!

and java Getjar will work because it will find both Getjar.class as well as myApp.Foo class

( PS: I am little confused about last statement that java command doesnt search for classes in current directory by default then why it gets executed??? )
maybe im forgetting something

Cheers for whatever youve understood!!! ???


VISHAL SAXENA
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37956
    
  22
Balu Sadhasivam wrote:I am confused on seeing the multiple "same" question. please use code tags.
Agree. What you posted is really difficult to read. Please go back and edit your first post by adding tags with the code button to the code, and also delete the line numbers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion over java classpath
 
Similar Threads
Mistake in GuideBook for SCJP Java 6.0, Chapter 10, Self Test question 11
chapter 10 question number 11 k&b book (on jar files)
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