aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes using classpath with javac Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "using classpath with javac Problem" Watch "using classpath with javac Problem" New topic
Author

using classpath with javac Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

KB book Q-3 Self test


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

-----------------------------------------------------------------------

i tried the above code

1.A option,B option gives the following compile time error:

cannot find symbol A

as far i know javac looks in the current directory by default.Then why such error

2.option c complies fine.but i think it's not a valid syntax

can anyone explain the what does the following line means:

. xcom/B.java

3.D and E gives the error:
file not found.

The D opton says " look in the directory xcom"
THe E option says "look in the directory xcom" and in the current directory too(i.e. test)

WHy is it not able to find B.java.THe xcom directory contains B.java





OCPJP 6.0 93%
OCPJWCD 5.0 98%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

1.A option,B option gives the following compile time error:
cannot find symbol A
as far i know javac looks in the current directory by default.Then why such error

It does search in the current directory but it will look for A.class in xcom directory in the current directory. Since you are already in the xcom directory, it will try to find /test/xcom/xcom/A.class which is not there so you get an error.

javac -classpath . xcom/B.java

In this command, javac is the command itself, -classpath . sets the classpath to the current directory. xcom/B.java tells javac to compile the B.java file in the xcom directory.

In D and E, it will try to look for B.java in the test directory. classpath is used to loop for .class files not .java files. You need to give javac command the correct path of the .java file yourself...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


Ankit thanks for reply,but i am still not able to get the answer.

i am confused that how to -cp works with java and javac ?


Queries:

1.Does -cp looks only for class files ?
2.Is it that javac looks for class files and java files in the current directory and java doesnot ?


3.

Why would
javac B.java

doesnot run ?

Ankit you said that
It does search in the current directory but it will look for A.class in xcom directory in the current directory. Since you are already in the xcom directory, it will try to find /test/xcom/xcom/A.class which is not there so you get an error.


why would it search xcom/A.class in /test/xcom/ ?
As A.class resides in xcom then,why is it no able to find it ?

--------
4.
javac -classpath . B.java
this statement says look in the current directory(xcom) for the file B.java and since B.java needs A.java,i think it could be found easily as it is in the xcom directory.


------------

5.
javac -classpath xcom B.java
javac -classpath xcom:. B.java

it
would not work since current directory is test and test contains xcom and not B.java.

Am i right ?

but ,i have one more doubt that i am specifying xcom in the classpath,then why it would not able to find A.class and B.java
is it that classpath only looks for .class files only.it would find A.class but not B.java ?


Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
@mohitkumar: Please see my post http://www.coderanch.com/t/505296/java-programmer-SCJP/certification/CLASSPATHS-please-critique-explanation-correct where I attempt to explain this same problem

If we SCJP6 aspirants (or any other student for that matter) try to explain each other concepts, I think it will deepen our understanding and appreciation of the subject at hand.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Sandra Bachan ,thanks for the post but my question still remain unanswered.
I am still not able to understand.

can somebody answer my queries ?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

mohitkumar gupta wrote:1.Does -cp looks only for class files ?
2.Is it that javac looks for class files and java files in the current directory and java doesnot ?

1. java command doesn't have anything to do with .java files.
2. javac looks for class files and java files in the current directory by default. If you use the -cp option or if the classpath environment variable is set, then javac doesn't look in the current directory automatically. It only looks into the classpath. Lets take a look at an example. Create two .java files A.java and B.java in a directory /example/test. The content of A.java and B.java will be
Now from your command prompt go to /example/test directory. Now run this command
javac -cp .. B.java
Now since we have set the classpath to the parent directory i.e. /example, so javac won't look for the class A in the current directory i.e. /example/test, so your code will fail to compile as A class is not found.


Why would
javac B.java

doesnot run ?

why would it search xcom/A.class in /test/xcom/ ?
As A.class resides in xcom then,why is it no able to find it ?

Since you run that command from xcom directory, when the compiler encounters B.java uses A class, the compiler would search for A class in java.lang package. Since its not found the compiler will think that A class might be in xcom package. Now the compiler has to look for xcom.A class. The compiler doesn't care if the current directory is xcom. The compiler knows that A class must be in xcom package so it will look for A class in xcom directory. Since you are already in xcom directory, the compiler will look for xcom directory in xcom directory i.e. /test/xcom/xcom. This is the way compiler works. If we tell the compiler to look for com.example.A class, it will look for a folder named com in the current directory. It doesn't matter if you run the command from /com/example folder, the compiler will then look for /com/example/com/example directory.

javac -classpath . B.java
this statement says look in the current directory(xcom) for the file B.java and since B.java needs A.java,i think it could be found easily as it is in the xcom directory.

Again here you are in the xcom directory. The compiler will look for xcom directory for A class in the classpath i.e. the current directory. So the compiler will look for /test/xcom/xcom/A.java or /test/xcom/xcom/A.class


javac -classpath xcom B.java
javac -classpath xcom:. B.java

it would not work since current directory is test and test contains xcom and not B.java.

Yes this is right
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

but ,i have one more doubt that i am specifying xcom in the classpath,then why it would not able to find A.class and B.java
is it that classpath only looks for .class files only.it would find A.class but not B.java ?




thanks,Ankit

please answer my last query.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using classpath with javac Problem