Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package problem

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
foo
|
test
|
xcom
|--A.class
|--B.java
And these two files:

package xcom;
public class A{}

package xcom;
public class B extends A{}

Please explain why does the following invocation does not work?

Set the current directory to test then invoke:
javac -classpath xcom:. B.java
 
Himai Minh
Ranch Hand
Posts: 1316
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javac looks throu classpath (xcom and current path), but it cannot find B.java
The command is : javac -classpath xcom:. xcom/B.java

When using javac to compile a file, specify the exact path of the file you want to compile. The classpath only specify the path where the supporting class is, which is A.class.
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:The javac looks throu classpath (xcom and current path), but it cannot find B.java
The command is : javac -classpath xcom:. xcom/B.java

When using javac to compile a file, specify the exact path of the file you want to compile. The classpath only specify the path where the supporting class is, which is A.class.

So you mean to say that classpath can help locate only .class files and not .java files? Or am I wrong?
 
Konrad Till
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himai Minh wrote:The javac looks throu classpath (xcom and current path), but it cannot find B.java
The command is : javac -classpath xcom:. xcom/B.java

When using javac to compile a file, specify the exact path of the file you want to compile. The classpath only specify the path where the supporting class is, which is A.class.


If xcom would have been the current directory then i guess the foll. would work -
javac . B.java
i guess so. OR if the import xcom.A; would have been there in class A source code. please rectify if i am wrong.
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarvesh dikonda wrote:
Himai Minh wrote:The javac looks throu classpath (xcom and current path), but it cannot find B.java
The command is : javac -classpath xcom:. xcom/B.java

When using javac to compile a file, specify the exact path of the file you want to compile. The classpath only specify the path where the supporting class is, which is A.class.


If xcom would have been the current directory then i guess the foll. would work -
javac . B.java
i guess so. OR if the import xcom.A; would have been there in class A source code. please rectify if i am wrong.


javac . B.java this won't work. i suppose you really wanted to say javac -classpath . B.java. assuming you forgot to mention the classpath switch , still it wont work. why ? it will be able to find B.java but what about A.class. you gave the classpath as current directory i.e. xcom and it will search for A in xcom like this - it will look for directory named xcom which contains A.class, but it won't find xcom in xcom. as a general rule the classpath should contain the PARENT OF THE PACKAGE ROOT. now since the package of A is xcom and the parent of xcom is test, the test directory should have been in classpath .so the command would be

javac -classpath ../ B.java (assuming you are in xcom directory).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic