• 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

Packages and Classpath

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
default classpath /foo

foo -> test -> xcom -> A.class
-> B.java

Two files :


package xcom;
public class A {}

package xcom;
public class B extends A {}

Which allows B.java to compile?

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


This question is from K&B.
The Answer is C.

While running A and B - I got " Cannot find symbol A ie. public class B extends A "

And running D and E - I got " Cannot read B.java"


Can somone tell me what differnece does the Classpath make for a file with a package and for a file with Out a package.

If a file did not have a package , I would simply run the file MyFile.java from the current directory which is set in my classpath

Classpath = C:\classfiles;
C:\Classfiles> javac MyFile.java



If both the files are located in xcom , why can't we invoke the command from xcom rather then Test.

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


It says that the classpath is set to the current directory which is Test.
Then how come it finds the A.class file which is present in xcom and not in Test.

Can some one please clear out the relation between classpath and Packages.


Thanks...
 
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
In Java, classes are identified by their qualified names. (See JLS - 6.7 Fully Qualified Names and Canonical Names.) If a class is part of a named package, the qualified name of the class is packageName.ClassName.

You have class files A and B in the directory "xcom," but Java is looking for these classes by their qualified names, xcom.A and xcom.B, which are found in the directory "test" -- not in "xcom."

In this example, B extends A. So when you compile B.java, you need to provide a classpath pointing to the "test" directory so that it can find the class xcom.A. Option 'C' is one way to do this.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...Got it!
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
here is my issue and it might be looking simple for those who know it..
i have found places where people use classpath while compiling and also while running the class file. to my knowledge i know that classpath is to say jvm that the particular class file is located in the specified location and we want that to be RUN by it. but why do we use classpath when compiling???
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While compiling you may want to set the classpath so that the compiler can resolve dependencies in your class.

If you are strictly using JSE API in your class you can leave it out.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic