Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B chapter 10 Q5 classpath question

 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
5.
Given the default classpath:
/foo
And this directory structure:



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


The answer is C. But I tested with this command in test dir:

javac xcom/B.java

It also compiles. I did NOT specified classpath as the answer gives. Does it contradict to what the book states as follows?

When searching for class files, the java and javac commands don't search the current directory by default.



 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is able to search for the class file, because of the package statement in the code.

I posted a similar question couple of days back, it might help you.
Search Class File

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not specify any -classpath and your system does not have any CLASSPATH env variable than java and javac by default takes current directory as a classpath.

This means to compile using

javac xcom/B.java

you must have been in the test directory, as B.java needed xcom/A.class file that is in the test directory.
Means test must be your current directory.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jia, this exact same question has been asked and discussed before in the forum. Please do a search for "xcom" and you'll find the older discussions about this.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I took Jesper's suggestion and did a search and found Ruben's post:
http://www.coderanch.com/t/426283/Programmer-Certification-SCJP/certification/Classpath-setting-involving-java-javac

The javac document says:
http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html
-cp path or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This classpath overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.


That explains my question.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did more test on the classpath issue, here is how:

directory structure:


A.java:


B.java:


After I did some test on compile and running the program, I found some really weird command combinations. The following commands are all tested on my machine:
(I'm using jdk 1.6.0_3)

javac command:

1) current directory is test:
javac com/xcom/B.java
javac -cp . com/xcom/B.java

2) current directory is com:
javac -cp .. xcom/B.java

3) current directory is xcom:
javac -cp ../.. B.java

java command:

1) current directory is test:
java com.xcom.B
java com/xcom/B
java com/xcom.B
java com.xcom/B

java -cp . com.xcom.B
java -cp . com/xcom/B
java -cp . com/xcom.B
java -cp . com.xcom/B


2) current directory is com:
java -cp .. com.xcom.B
java -cp .. com/xcom/B
java -cp .. com/xcom.B
java -cp .. com.xcom/B

java -cp .. xcom.B
java -cp .. xcom/B

3) current directory is xcom:
java -cp ../.. com.xcom.B
java -cp ../.. com/xcom/B
java -cp ../.. com/xcom.B
java -cp ../.. com.xcom/B

java -cp ../.. xcom.B
java -cp ../.. xcom/B

Basically, you need to include one level higher than the top package directory unless it's already current directory

 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's interesting, Jia. Thanks for posting.

The only ones that don't work for me are:
2) current directory is com
java -cp .. xcom.B
java -cp .. xcom/B

3) current directory is xcom
java -cp ../.. xcom.B
java -cp ../.. xcom/B

It makes sense these doesn't work because it's looking for a class xcom.B, when the class is actually com.xcom.B.

I guess that com.xcom.B, com/xcom.B, com.xcom/B, and com/xcom/B are all treated the same. It seems the only thing java cares about is that the package defined in the class file and the directory structure containing the file are the same.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I re-run the commands, they did worked on my machine. The environment on this machine is:
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03
PATH=%JAVA_HOME%\bin (yes, I remove all other path entries)
All the commands I gave worked.

I tried on another computer, not only the commands you pointed out does not work, but also the commands without explicit -classpath won't work. I tried to include current directory (.) in the CLASSPATH environment variable, and the commands without explicit -classpath back on work again. What could be wrong with my settings?

Please if someone can run those commands on your machine and shed some lights on it. TIA
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also getting the result what Ruben has got.
Show me the output of : echo %classpath% on your computer.

 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit,

I didn't set CLASSPATH on my machine. echo %classpath% simply returns "%classpath%"
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jia Tan wrote:Punit,

I didn't set CLASSPATH on my machine. echo %classpath% simply returns "%classpath%"


See your directory structure on your file system, just confirm whether there is any duplicate B.java and A.java that are residing in just package xcom; under test directory.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why! Thank you Punit! I was testing K&B original example which has xcom under test, and in that directory there are A.class and B.class! Later I created com/xcom file structure under same test directory, and thing started getting confused! After I remove the xcom under test, everything back to normal.

I am still wondering why my other computer don't accept current dir why I didn't specify -classpath in javac or java, I will do more research on that.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jia Tan wrote:That's why! Thank you Punit! I was testing K&B original example which has xcom under test, and in that directory there are A.class and B.class! Later I created com/xcom file structure under same test directory, and thing started getting confused! After I remove the xcom under test, everything back to normal.

I am still wondering why my other computer don't accept current dir why I didn't specify -classpath in javac or java, I will do more research on that.


Your other computer must have CLASSPATH variable set to something.
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That machine is not at my hand now, I will check later. Thank you Punit for all the help!
 
Jia Tan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just checked the other machine, yes the classpath environment variable did exists with qt.jar, I believe it was set when I install quicktime. Anyways, after I remove the variable, javac and java start to pick up current directory by default. Thank agian to Punit for you insight!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic