• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Classpath

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No Ruben, I think if this one were given,



then it will be valid.

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac -classpath . xcom/B.java

what does this line imply??
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but Punit was correcting the mistake in my explanation.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


What does this mean???
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes he is right.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i change option 4 to

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

then B.java would compile right?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


One more doubt,
javac -classpath xcom:. B.java ( current directory is test) what does this line imply?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that means in this case,
-classpath is redundant. The classpath is not set. It is the default /foo?
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
javac xcom/B.java
current directory is test. classpath is/foo. I think this allows B.java to compile?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Punit, this is the correct explanation or the one given by Sachin .i am totally confused in case4 and case5.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic