Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

confusion about classpath

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All, Please help me to understand how I can remember the classpath questions/situations.

What i understand is ...
And this directory structure:
foo
|
test
|
xcom
|--A.class
|--B.java

And these two files:

package xcom;
public class A { }
package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)
A. Set the current directory to xcom then invoke
javac B.java
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
D. Set the current directory to test then invoke
javac -classpath xcom B.java
E. Set the current directory to test then invoke
javac -classpath xcom:. B.java


C is the correct answer.... But how Do i remember the basics of setting classpath ...
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B extends A, so in order for B to compile, it must be able to find the class file for A.

Here's the thing to consider: A is in the package xcom, so its qualified class name is xcom.A. This is in the directory test (not xcom).

So the classpath should be the test directory relative to the current directory.
 
Avi Sridhar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
B extends A, so in order for B to compile, it must be able to find the class file for A.

Here's the thing to consider: A is in the package xcom, so its qualified class name is xcom.A. This is in the directory test (not xcom).

So the classpath should be the test directory relative to the current directory.



Thank You
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused that how come the A.class will be found in this scenario.

We are presently in Test Directory as per the question.

The classpath has been set to current directory and that means that we are in Test directory not in XCOM directory where A.class file is actually present.
How come option "C" is correct ???
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by hello sharma:
... The classpath has been set to current directory and that means that we are in Test directory not in XCOM directory where A.class file is actually present...


But the name of this class is not just "A." Java classes are identified by their qualified names. As explained in JLS - 6.7...

  • The fully qualified name of a top level class or top level interface that is declared in an unnamed package is the simple name of the class or interface.
  • The fully qualified name of a top level class or top level interface that is declared in a named package consists of the fully qualified name of the package, followed by ".", followed by the simple name of the class or interface.
  • This means that if a class is in an unnamed package (which is the default if there is no package declaration), then the qualified name is simply the class name. But if there is a package declaration, then the qualified name is the package name, followed by a dot, followed by the class name.

    In this example, A is in the xcom package, and this gives it a qualified name of "xcom.A." Because it's in this package, we cannot refer to it simply as "A" in the "xcom" directory. Instead, we need to refer to it as "xcom.A," which is in the "test" directory.
     
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    But, why using / before B.java instead of xcom B.java
    that means D should be the answer and not C.
     
    Lao Kinsuyi
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    But, why using / before B.java and not xcom B.java So, i think D should be the correct answer and not C.
     
    marc weber
    Sheriff
    Posts: 11343
    Mac Safari Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Lao Kinsuyi:
    But, why using / before B.java instead of xcom B.java
    that means D should be the answer and not C.


    Java classes are identified by their qualified names. But Java source files are different. When you compile with javac, you just provide a path to the .java file. So if "test" is the current directory, this would be xcom/B.java (assuming the forward slash is a path delimiter).

    Also, note that option D is setting the classpth to "xcom" (which does not contain the class xcom.A), and then trying to compile B.java from the "test" directory (which does not contain B.java).
     
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "hello sharma",
    Please check your private messages.
    -Ben
     
    reply
      Bookmark Topic Watch Topic
    • New Topic