• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question and sample problem about classpath

 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Sorry if this question has been asked before, but I could not find relevant answers:

// This is B.java
package pkg;
public class B {}
------------------
// This is A.java
package pkg;
public class A { B b; }

directory structure:

/proj/pkg/A.java
/proj/pkg/B.java


and I am in /proj directory: I am able to compile A.java in one shot: "javac pkg/A.java"

If I go to /proj/pkg directory, and compile B.java first ("javac -d . B.java"; it create a recursive "pkg" folder), then I would be able to compile A.java; But I won't be able to compile A.java in one shot (i.e. before compiling B.java) like the previous scenario.

Thank you very much for your comments
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
real test wrote:and I am in /proj directory: I am able to compile A.java in one shot: "javac pkg/A.java"


'What do you mean by one shot?
what is the error you are getting while compiling A
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean to be able to compile A.java and B.java in one javac command such as "javac -d . A.java" (as opposed to compiling B.java first and then compiling A.java).... I am not able to do that and I get:

A.java:2: cannot find symbol
symbol : class B
location: class pkg.A
public class A { B b; }→
^
1 error


Thanks,
 
shivendra tripathi
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try javac -cp ../ A.java from pkg directory
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! it worked!
I haven't seen that before ("../")
What does it mean? I mean I know "." means current directory and ".." means one directory up, and for example ../bin means one directory up and then inside bin directory. But what does "../" mean? How is that different from just "." (the current directory)

Thank you very much
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"real test" please check your private messages for some administrative matter.
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit,
Changed display name : T.Adams.

Thanks
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what does "../" mean?

You gave the answer yourself. It means the parent directory. So if you run the command from /foo/foofoo, then ../ means /foo
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Shivendra and Ankit
I think I got confused there, sure "../" and ".." both mean one level up, and that makes sense.

However I am having trouble to understand the -d option with javac:

In the above example, from /proj/pkg directory "javac -d . A.java" won't work (I thought -d . makes the B.class to go under /proj/pkg/pkg too, and "javac -d . A.java" will work in one shot, since there will be a /proj/pkg/pkg/B.class and A.java can compile fine; but it seems like it's not the case? I mean in this case does -d only applies to A.java?

If that's the case then why when I say "javac -cp ../ -d . A.java" from /proj/pkg directory, both A.class and B.class go under /proj/pkg/pkg?
Shouldn't the B.class go under /proj/pkg, and A.class go under /proj/pkg/pkg (since -d applies to A.java only and not the B.java)?



I think I might not have a clear picture of what is going on specially in these cases when compiling one class needs compilation of intermediate classes first..

Any input and clarification would be highly appreciated...
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the above example, from /proj/pkg directory "javac -d . A.java" won't work (I thought -d . makes the B.class to go under /proj/pkg/pkg too, and "javac -d . A.java" will work in one shot, since there will be a /proj/pkg/pkg/B.class and A.java can compile fine; but it seems like it's not the case?

Its quite unclear what you are trying to say. I went to pkg directory, and compiled B.java as javac -d . B.java. Then remaining in pkg folder, I compiled A.java as javac -d . A.java and it also worked. Did it not work on you system??

If that's the case then why when I say "javac -cp ../ -d . A.java" from /proj/pkg directory, both A.class and B.class go under /proj/pkg/pkg?
Shouldn't the B.class go under /proj/pkg, and A.class go under /proj/pkg/pkg (since -d applies to A.java only and not the B.java)?

No, the -d flag applies to both A.java and B.java. since B.java was not already compiled, so the compiler had to compile it. So the -cp ../ doesn't effect where B.class goes...
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I went to pkg directory, and compiled B.java as javac -d . B.java. Then remaining in pkg folder, I compiled A.java as javac -d . A.java and it also worked


True. The question is while being in pkg directory, why we can't compile A.java in one shot (i.e. without compiling B.java first) with this command: "javac -d . A.java"

Thank you,
God bless you,
Cheers
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is while being in pkg directory, why we can't compile A.java in one shot

Because we are in the wrong directory. We are in the pkg directory and the compiler now tries to look for pkg/B.class but there's no such file. So it will try to find pkg/B.java, but that's also not there as we are already in the pkg directory so there is no pkg directory in pkg directory. But if we use -cp ../ , then the compiler is able to find pkg/B.java and thus compile the program...
 
T. Adams
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see. That makes sense. I think I am starting to grasp this more and more now.

Thank you very much (specially you Ankit).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic