Tuesday, August 3, 2010

Classpath: B.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 10, Question 3

Given the default classpath:


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 and 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 the invoke:

javac -classpath xcom B.java

E. Set the current directory to test then invoke:

javac -classpath xcom:. B.java

A. B.java depends on A.class to compile. Since A.class is part of xcom package, you need to be one directory above xcom because compiler won't see A.class
B. Similar explanation for why option A is incorrect. Setting the classpath to current directory, i.e. xcom makes no difference because A.class is part of xcom package
C. This is CORRECT. Move up to test directory, set the classpath to current directory by typing in "." and then type the relative path of the program to compile, i.e, xcom/B.java
D. Command works the similar way as option A and B. Even though you are in test, you are setting classpath to xcom and compiler won't see A.class
E. You are setting the classpath to xcom, and current directory. This command would work if you invoke xcom/B.java instead of B.java

NOTE: Please check how to set classpath for your Operating System. If you are on UNIX-based OS, : is the classpath separator, but if you are on Windows-based OS, ; is the classpath separator.

No comments:

Post a Comment