• 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

classpath question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Q 3 on page 812 K&B,

Default classpath is : /foo

Directory structure is :


And these two files:



Which allows B.java to compile?

B. Set the current directory to xcom, then invoke javac -classpath . B.java
C. Set the current directory to test, then invoke javac -classpath . xcom/B.java

The answer says C is correct. But I think B should be correct.

Since class B extends A, to compile B.java compiler needs A.class
classpath is used only to search for .class files, not .java files. So in our example, we need classpath to get us to A.class

Based on these, Lets consider option B
We are in xcom directory
the classpath is "." , the current directory, "xcom" and A.class files is in xcom directory
And B.java is also in xcom directory

So B is correct.

In option C, we are in "test" directory.
the classpath is "." current directory "test" So classpath should have been "xcom" as A.class is in xcom
So C is incorrect.


Kindly explain... Thank you.
 
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,

Please note that the A and B have a package declaration xcom. We need to take that into account. In case A and B did not have the package declartion xcom then B could have been correct.

Regards,
Vijay
 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijay keshava wrote:
Please note that the A and B have a package declaration xcom.



How does that affect it?
package xcom; only means A.java and B.java both will be in package/directory xcom. How does that make option C correct?
 
Vijay keshava
Ranch Hand
Posts: 40
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
pacakge xcom also means that the fully qualified class name is xcom.C. When executing a program java looks for the fully qualified class name from the entries mentioned in the CLASSPATH. Hence the root folder of the package name should be in the CLASSPATH, which in this example is foo/test. This is what is specified in the language - the rules of language.

Let us say that java would have allowed you to execute class C from directory xcom. Let us assume that there is class xtest.Z that class C refers to. Now you are in directory xcom. Where should java search for class Z? Also what entries should the CLASSPATH now have? Think about an application having multiple packages and multiple files...

Would it not be simpler if all the commands executed from the root directory?

 
Peter Swiss
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, thank you Vijay!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic