wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpath" Watch "Classpath" New topic
Author

Classpath

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: K&B.



Here case1. is not possible as the classpath is valid only upto foo, so we cannot invoke javac inside a sub-directory of foo, Am I right? But what does case2- case5 imply??
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Abhi,

Only answer 3. is correct. Why? Because you must include test in the classpath. That's the only answer that includes test in the classpath. Do you understand why test must be in the classpath?


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

Joined: Oct 16, 2008
Posts: 952
No Ruben, I think if this one were given,



then it will be valid.


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
javac -classpath . xcom/B.java

what does this line imply??
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You are absolutely right, Punit. Both classes are in the same package (so there is no import necessary from B.java) and we are just requiring compilation. Thanks for clarifying that.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
The explanation in the book:
C is correct. In order for B.java to compile, the compiler first needs to be able to find B.java. Once it's found B.java it needs to find A.class. Because A.class is in the xcom package the compiler won't find A.class if it's invoked from the xcom directory. Remember that the -classpath isn't looking for B.java, it's looking for whatever classes B.java needs (in the class A.class).
A, B and D are incorrect based on the above. E is incorrect because the compiler can't find B.java.

So.
1. cannot find A.class
2. again cannot find A.class
3. correct
4. cannot find A.class(There's a typo in the question . The correct version is : Set the current directory to test then invoke javac -classpath xcom B.java)
5. cannot find B.java

One thing to remember compiler is always looking for xcom.A and not A


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:javac -classpath . xcom/B.java

what does this line imply??


This means you are saying compiler to find xcom package in current directory, this is optional no need to say this as javac by default will find xcom in current directory.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Abhi vijay wrote:javac -classpath . xcom/B.java

what does this line imply??

That means you are putting the current directory (.) in the classpath, in this case test, and then compiling B.java which is in the xcom subdirectory of test. But like Punit explained, it is not necessary to have test in the classpath in this case, because since both B and A are in the same package, there are no imports necessary from B.java. However, running will be a different story. Assuming that B had a main method, you would need to put test in the classpath and then specify xcom.B as the class to be run.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit Singh wrote:No Ruben, I think if this one were given,



then it will be valid.



But, this one is not in the options.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yes, but Punit was correcting the mistake in my explanation.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Sachin Adat,
One thing to remember compiler is always looking for xcom.A and not A

What does this mean???


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Ruben Soto wrote:
Only answer 3. is correct. Why? Because you must include test in the classpath. That's the only answer that includes test in the classpath. Do you understand why test must be in the classpath?

For finding xcom.A you need to have test in classpath or atleast you have to be in test directory which Punit pointed out, but it is not in the options.
So I think you were right.

Correct me if I am wrong.....

Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
James Tharakan wrote:
Sachin Adat,
One thing to remember compiler is always looking for xcom.A and not A

What does this mean???

It means after a package declaration the name of the class becomes package.class which is atomic, which we had discussed in the other post yesterday.....
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes he is right.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
James Tharakan wrote:
Sachin Adat,
One thing to remember compiler is always looking for xcom.A and not A

What does this mean???


This means, class A is in package xcom, so it is not just A, now it has become xcom.A, its fully qualified name.

However when you compile class A, you can cd to xcom directory and can call:
javac A.java

But you cannot call

javac B.java

as javac B.java will try to find xcom.A in the xcom directory.
F:\workspace\scjp\src\xcom>javac A.java

F:\workspace\scjp\src\xcom>javac B.java
B.java:3: cannot find symbol
symbol: class A
public class B extends A{
^
1 error


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Sachin Adat wrote:
Ruben Soto wrote:
Only answer 3. is correct. Why? Because you must include test in the classpath. That's the only answer that includes test in the classpath. Do you understand why test must be in the classpath?

For finding xcom.A you need to have test in classpath or atleast you have to be in test directory which Punit pointed out, but it is not in the options.
So I think you were right.

Correct me if I am wrong.....


Sorry Sachin, I'm too tired to know at this point (U.S. time is 23:48.) I will delegate on Punit the final decision on this, as he always seems to be right.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
if i change option 4 to

javac - classpath xcom/B.java (current directory is test)

then B.java would compile right?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Abhi vijay wrote:if i change option 4 to

javac - classpath xcom/B.java (current directory is test)

then B.java would compile right?


I don't think so. You have not provided any parameter to the classpath option. If you don't want to set the classpath, then just use plan javac command

javac xcom/B.java


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Abhi vijay wrote:if i change option 4 to
javac - classpath xcom/B.java (current directory is test)
then B.java would compile right?

Its a typo, I had mentioned it earlier.

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
The final and useful tip to solve this question from K & B is to see in this way

The compiler is always looking for xcom.A and not A
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:

One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?


This imply find B.java in xcom directory or in current directory, but there is no B.java in xcom directory as it is xcom/B.java, and there is no B.java in current directory as it is actually xcom/B.java.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Prav sharma wrote:The final and useful tip to solve this question from K & B is to see in this way

The compiler is always looking for xcom.A and not A


then why this works?



cd xcom
javac A.java



Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

well you can compile a .java file even if it is not in the correct folder hierarchy. The javac command will put it into the correct hierarchy in that case. Suppose I have this class

package my.package;
class MyClass{}

Now if I put it under directory test and compile it from test directory, the MyClass.class file will go into the directory test/my/package.

But if I put MyClass in directory test/my/package and compile it from the test folder as javac my/package/MyClass.java, then the MyClass.class file will be placed along the MyClass.java file.

This behavior of the javac command can be altered using the -d option...
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
that means in this case,
-classpath is redundant. The classpath is not set. It is the default /foo?
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit Singh wrote:
Abhi vijay wrote:

One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?


This imply find B.java in xcom directory or in current directory, but there is no B.java in xcom directory as it is xcom/B.java, and there is no B.java in current directory as it is actually xcom/B.java.

I guess you are wrong here Punit.
B.java is in xcom directory.
-classpath is for finding class files not java files,
So in the above case, javac first tries to find B.java in test directory which it is unable to find.
classpath is set to xcom and .
Had it found B.java it would have compiled because the classpath setting is correct for finding xcom.A when classpath is set to .
I think till the java file is compiled its still just the name so in this case B.java.
Had the command been javac -classpath xcom:. xcom/B.java(with current directory test), this could also have been a valid answer.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
javac xcom/B.java
current directory is test. classpath is/foo. I think this allows B.java to compile?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:javac xcom/B.java
current directory is test. classpath is/foo. I think this allows B.java to compile?


Ya it will compile.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:
Punit Singh wrote:
Abhi vijay wrote:

One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?


This imply find B.java in xcom directory or in current directory, but there is no B.java in xcom directory as it is xcom/B.java, and there is no B.java in current directory as it is actually xcom/B.java.

I guess you are wrong here Punit.
B.java is in xcom directory.
-classpath is for finding class files not java files,
So in the above case, javac first tries to find B.java in test directory which it is unable to find.
classpath is set to xcom and .
Had it found B.java it would have compiled because the classpath setting is correct for finding xcom.A when classpath is set to .
I think till the java file is compiled its still just the name so in this case B.java.
Had the command been javac -classpath xcom:. xcom/B.java(with current directory test), this could also have been a valid answer.


Thanks Sachin, I was missing the point that classpath is for finding class files not java files.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankit Garg wrote:well you can compile a .java file even if it is not in the correct folder hierarchy. The javac command will put it into the correct hierarchy in that case. Suppose I have this class

package my.package;
class MyClass{}

Now if I put it under directory test and compile it from test directory, the MyClass.class file will go into the directory test/my/package.




This is not working for my windows xp Ankit. It is putting MyClass.class in test directory, not under test/my/package directory.
I have used only this command:



Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Oops! I said the opposite thing :P . If you use the -d option, then it will put the .class file into the correct hierarchy...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:

One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?


This will not compile Abhi, as B.java is in the xcom directory, not in the current directory, you should write this one:
javac -classpath xcom:. xcom/B.java

If you are using windows then this one:
javac -classpath xcom;. xcom/B.java
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Punit, this is the correct explanation or the one given by Sachin .i am totally confused in case4 and case5.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Sachin Adat wrote:
-classpath is for finding class files not java files,
So in the above case, javac first tries to find B.java in test directory which it is unable to find.
classpath is set to xcom and .
Had it found B.java it would have compiled because the classpath setting is correct for finding xcom.A when classpath is set to .
I think till the java file is compiled its still just the name so in this case B.java.
Had the command been javac -classpath xcom:. xcom/B.java(with current directory test), this could also have been a valid answer.


I guess, me and Punit are saying the same thing here ..........
So what are you confused at?


Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Need to make a correction..........to my 1st message
4. cannot find A.class

Sorry I just blindly followed the book.
Case 4 is incorrect because it cannot find B.java
It is not the correct answer though for the other reason.
Is this error big enough to be an errata..........Is there an errata for SCJP 6 by K&B.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
javac -classpath xcom : . B.java.

when this line is invoked, the compiler searches for B.java in xcom directory, it finds it but cannot find xcom.A.
So now the classpath is set to (.), that is the test directory, here xcom.A is found but B.java cannot be found. So fails.


Now when I change

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

when this line is invoked, the compiler searches for B.java in xcom directory, it finds it but cannot find xcom.A.

then So now the classpath is set to (.), that is the test directory, here beacuse of the sub-directory xcom/B.java, the compilation suceeds.

This is waht I have gathered from Sachin and Punit's posts.
Am I right,???
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Abhi vijay wrote:javac -classpath xcom : . B.java.
when this line is invoked, the compiler searches for B.java in xcom directory, it finds it but cannot find xcom.A.

Wrong
Hope these will make things clearer.........
1. Forget about classpath before finding the java file.
2. First check the current directory and see if you can reach to the java file. If no you get file not found error.
3. If yes to the 2nd point, then you check for class files needed for your java file to compile and classpath comes into pitcure.
In this case we already have A.class in xcom directory so A.java is not needed. But for finding it, you need to have test in the classpath which has xcom.A
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
also....
Abhi vijay wrote:javac xcom/B.java
current directory is test. classpath is/foo. I think this allows B.java to compile?

No, this will not be able to find A.class
test has to be in classpath.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:also....
Abhi vijay wrote:javac xcom/B.java
current directory is test. classpath is/foo. I think this allows B.java to compile?

No, this will not be able to find A.class
test has to be in classpath.


Why Sachin? I think B.java will be able to find xcom.A class as xcom package is in test directory itself.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:javac -classpath xcom : . B.java.

when this line is invoked, the compiler searches for B.java in xcom directory, it finds it but cannot find xcom.A.
So now the classpath is set to (.), that is the test directory, here xcom.A is found but B.java cannot be found. So fails.


Now when I change

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

when this line is invoked, the compiler searches for B.java in xcom directory, it finds it but cannot find xcom.A.


then So now the classpath is set to (.), that is the test directory, here beacuse of the sub-directory xcom/B.java, the compilation suceeds.

This is waht I have gathered from Sachin and Punit's posts.
Am I right,???


Abhi, do you have unix machine?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath
 
Similar Threads
Ch. 10 Development question
Doubt in classpath statement....
Plz help me in solving the problem of JAVAC comand
Classpath Related Question
Classpath - Question 1