aspose file tools*
The moose likes Beginning Java and the fly likes Question regarding javac command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question regarding javac command" Watch "Question regarding javac command" New topic
Author

Question regarding javac command

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Following is a self test question in the "development" chapter of SCJP for java 6 by Kathy-Bert:

3. Given the default classpath:

/foo

And this directory structure:

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

And these two files:



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

Why can't I say javac B.java from xcom directory? It should find the B.java source and the A.class too. Correct? Or is it mandatory that whatever package the class is in, I will have to specify the same while invoking javac i.e. in this case I would have to say xcom/B.java?

The correct answer according to the book is option "C". How? If we are in test directory, and we say javac -classpath . xcom/B.java, how will it find A.class which is inside package xcom.

~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
So, which do you think, and which did the book say, are correct?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I tried the example practically. The book is correct. My answer was B. Why can't we say Since we are in xcom directory, -classpath . should refer to A.class and B.java should be fine. Why not?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Not sure. Probably because it isn’t A.class, but xcom.A.class. Because of the prefix (package name), it is only identifiable from outside the package structure.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Both the required class file(A.class) and source file(B.java) are in package xcom. So why only the source requires that fully qualified name should be provided? Why doesn't the compiler recognize the fact that we are already at xcom level and search for source B.java?
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Hi,

I think the current directory matters. The compiler assumes the current directory is start point for everything, so when it finds a class A declared in the package xcom, it will try to find the A.java file under xcom directory under current directory. That's why we must set the current directory to "test".


PM Me If You Want to get Urgent Help on Java Programming
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
How is the compiler supposed to know it is in xcom? You open it from the command line/terminal and the command line/terminal might not pass that information to it.
Niraj Jha
Ranch Hand

Joined: Feb 20, 2013
Posts: 63

see how the compiler works stepwise:-

xcom>javac -verbose -classpath . B.java

Use same -verbose with all the options , and see how the compiler looks for the file in current directory.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Niraj Jha, welcome to the Ranch
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Niraj Jha wrote:see how the compiler works stepwise:-

xcom>javac -verbose -classpath . B.java

Use same -verbose with all the options , and see how the compiler looks for the file in current directory.




Hi Niraj

I did as you said. I tried executing the above command from the xcom directory with -verbose flag. I got the following error:

C:\projects\workspace\xcom>javac -verbose -classpath . B.java
[parsing started B.java]
[parsing completed 12ms]
[search path for source files: .]
[search path for class files: C:\Java\jdk1.6.0_38\jre\lib\resources.jar,C:\Java\jdk1.6.0_38\jre\lib\rt.jar,C:\Java\jdk1.6.0_38\jre\lib\sunrsasign.jar,C:\Java\jd
k1.6.0_38\jre\lib\jsse.jar,C:\Java\jdk1.6.0_38\jre\lib\jce.jar,C:\Java\jdk1.6.0_38\jre\lib\charsets.jar,C:\Java\jdk1.6.0_38\jre\lib\modules\jdk.boot.jar,C:\Java
\jdk1.6.0_38\jre\classes,C:\Java\jdk1.6.0_38\jre\lib\ext\dnsns.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\localedata.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunjce_provide
r.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunmscapi.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunpkcs11.jar,.]
B.java:2: cannot find symbol
symbol: class A
public class B extends A{}→

^
[checking xcom.B]
[total 86ms]
1 error

My question is when I have provided the dot(.) against the -classpath flag, it should find the A.class file in the current directory as dot tells the compiler to do so. But it is unable to do so. Why?

And while I was playing around with the command, I tried giving javac -verbose -classpath .. B.java and voila, it worked. I received the following verbose:

[parsing started B.java]
[parsing completed 11ms]
[search path for source files: ..]
[search path for class files: C:\Java\jdk1.6.0_38\jre\lib\resources.jar,C:\Java\jdk1.6.0_38\jre\lib\rt.jar,C:\Java\jdk1.6.0_38\jre\lib\sunrsasign.jar,C:\Java\jd
k1.6.0_38\jre\lib\jsse.jar,C:\Java\jdk1.6.0_38\jre\lib\jce.jar,C:\Java\jdk1.6.0_38\jre\lib\charsets.jar,C:\Java\jdk1.6.0_38\jre\lib\modules\jdk.boot.jar,C:\Java
\jdk1.6.0_38\jre\classes,C:\Java\jdk1.6.0_38\jre\lib\ext\dnsns.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\localedata.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunjce_provide
r.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunmscapi.jar,C:\Java\jdk1.6.0_38\jre\lib\ext\sunpkcs11.jar,..]
[loading ..\xcom\A.class]
[loading java\lang\Object.class(java\lang:Object.class)]

[checking xcom.B]
[wrote B.class]
[total 90ms]

See the difference. When I gave 2 dots(..) instead of one (.), it was able to find the A.class file. I read that . represents the current directory and .. represents parent directory. What is going on here?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

I understood what is it that is going on here. Hence, I shall mark it as resolved. If anyone wants to understand the reason, read this nice article:

How the compiler resolves class dependencies using "package name=directory name" rule?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Yes, it is a nice article, but it suggests setting a system CLASSPATH, which is usually a mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question regarding javac command