aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question and sample problem about classpath 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 "question and sample problem about classpath" Watch "question and sample problem about classpath" New topic
Author

question and sample problem about classpath

T. Adams
Greenhorn

Joined: Nov 04, 2009
Posts: 10
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

Joined: Oct 27, 2009
Posts: 92
real [surl='http://www.javaranch.com/unit-testing.jsp' class='api' title='article: evil unit testing wrote:test[/surl]]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

Joined: Nov 04, 2009
Posts: 10
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

Joined: Aug 26, 2008
Posts: 263
Try javac -cp ../ A.java from pkg directory


SCJP 1.5(97%) My Blog
T. Adams
Greenhorn

Joined: Nov 04, 2009
Posts: 10
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

"real test" please check your private messages for some administrative matter.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
T. Adams
Greenhorn

Joined: Nov 04, 2009
Posts: 10
Thanks Ankit,
Changed display name : T.Adams.

Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Nov 04, 2009
Posts: 10
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Nov 04, 2009
Posts: 10
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Nov 04, 2009
Posts: 10
I see. That makes sense. I think I am starting to grasp this more and more now.

Thank you very much (specially you Ankit).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question and sample problem about classpath