Forums Register Login

package name can not be separated from class, . with -classpath

+Pie Number of slices to send: Send
package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

does it because as A is in package top it can only be searched from above the top directory


One more problem is :
the package top is in directory Desktop
so when i try this to compile A.java from directory desktop
javac -classpath top A.java
it says A.java can not found , classpath specifies its in top package then it should have compiled A.java

Last one:
Does specifying current directory (.) is must with -classpath option ,otherwise it doesn`t look into current directory

An answer that i can understand is required
+Pie Number of slices to send: Send
They are in the same directory then why do you need a classpath??
Top is the folder which has the java files.
first compile A.java and then B.java (keep you current dir as top and no need a classpath for that)

yes the (.) is required to find the files in the current directory. and there should be a semicolon after the "package top" in B.java
+Pie Number of slices to send: Send
 

Mahtab Alam wrote:package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

does it because as A is in package top it can only be searched from above the top directory


One more problem is :
the package top is in directory Desktop
so when i try this to compile A.java from directory desktop
javac -classpath top A.java
it says A.java can not found , classpath specifies its in top package then it should have compiled A.java



To compile A.java in your current desktop directory : javac top/A.java
To compile B.java in your current desktop directory javac top/B.java

To compile A.java in your current top directory : javac A.java
To compile B.java in your current top directory javac B.java
+Pie Number of slices to send: Send
Thanks for reply ,
I know those ways to compile .
I want to know why it doesn`t work with classpath

Because exam uses classpath option .and you know choose all that apply


What i want to know basically is
Say you are in directory A in which file run.class is ,now lets say we have jar file my.jar inside A directory .To run A.class it needs access to some class under my.jar
java -classpath my.jar A

or to find the file A.class under current directory i have to use a . also
so
java -classpath .:my.jar A

just thats it
+Pie Number of slices to send: Send
package top;
class A{}


package top
class B extends A{}

executing javac from within the top directory
javac -classpath . B.java
why this does not compile it needs A.class its their , B.java its their

Since you are in top directory, javac can find B.java, but it cannot find top.A
This is the most confusing part. Under top directory, there is an A class. Under desktop directory, there is a top.A class

In javac -classpath . B.java, javac first looks for top.A class in order to compile B. But under top directory, there is an A.class , not top.A.

-classpath should include the root of the class you are looking for.
Suppose you are under destkop directory and you are looking for top. A class, then javac -classpath . B.java works.
Suppose you are under top directory, your command should be javac -classpath ..\ B.java
It says going back to one level up to find top. A
Paper jam tastes about as you would expect. Try some on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 843 times.
Similar Threads
Classpath doubt k&b chp 10 que 5
using classpath with javac Problem
Classpath Query
how javac search file?
K&B Chapter 10, Self test doubt
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 21:58:10.