• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion over java classpath

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean to ask:

If the javac statement required the jar file then why not the java statement?
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!! ???

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic