File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B chapter 10 Q5 classpath question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B chapter 10 Q5 classpath question" Watch "K&B chapter 10 Q5 classpath question" New topic
Author

K&B chapter 10 Q5 classpath question

Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
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

Joined: Sep 28, 2008
Posts: 324
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


SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
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

Joined: Jan 28, 2009
Posts: 32
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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
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

Joined: Oct 16, 2008
Posts: 952
I am also getting the result what Ruben has got.
Show me the output of : echo %classpath% on your computer.

Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
Punit,

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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Jan 28, 2009
Posts: 32
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Jan 28, 2009
Posts: 32
That machine is not at my hand now, I will check later. Thank you Punit for all the help!
Jia Tan
Ranch Hand

Joined: Jan 28, 2009
Posts: 32
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: K&B chapter 10 Q5 classpath question
 
Similar Threads
Very Much Confused !!!
Ch. 10 Development question
Plz help me in solving the problem of JAVAC comand
Classpath Related Question
Classpath - Question 1