Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath - Question 1

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

The following question is from K&B Book.

Given the default classpath:

/foo

And this directory structure:


And these two files:

package xcom;
public class A { }

package xcom;
public class B extends A { }

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

The correct answer is C. But what would happen if I removed the package declaration from two files above still leaving them in the same directory ?

Would the correct answer be B ?
 
Greg Charles
Sheriff
Posts: 2984
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can always just try these problems to see how they work. Basically, this one comes down to needing the classpath to be foo/test and being able to find the B.java file we want to compile.

Which allows B.java to compile? (Choose all that apply.)
A. Set the current directory to xcom
then invoke javac B.java

No good. You said the classpath was set to foo, not foo/test

B. Set the current directory to xcom
then invoke javac -classpath . B.java

Also no good. Now the class path is foo/test/xcom.

C. Set the current directory to test
then invoke javac -classpath . xcom/B.java

Good one. The classpath is foo/test and we tell javac where to find the file to compile.


D. Set the current directory to test
then invoke javac -classpath xcom B.java

No good at all. The classpath is wrong and javac can't find the file.


E. Set the current directory to test
then invoke javac -classpath xcom:. B.java

No good. The classpath is OK despite the extra entry, but javac can't find the file.


The correct answer is C. But what would happen if I removed the package declaration from two files above still leaving them in the same directory ?

Would the correct answer be B ?


Yes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic