Faraz Kadri wrote:Given the following directory structure
x - |
| - FindBaz.class
|
| - test - |
| - Baz.class
| - myApp - |
| - Baz.class
And given the contents of the java files
public class FindBaz {
public static void main(String[] args) { new Baz(); }
}
In the test directory:
public class Baz {
static { System.out.println("test/Baz"); }
}
In the myApp directory:
public class Baz {
static { System.out.println("test/Baz"); }
}
If the current directory is x, which invocation will produce the output "test/Baz"?
The book states option B is wrong:
B. java -classpath test FindBaz
I would like to know why the option B is wrong? Does the location of FindBaz must be defined in classpath (as a '.' in this case)? I don't think we do since the program class is in the current directory.
Thanks in advance.
Faraz Kadri wrote:I would like to know why the option B is wrong? Does the location of FindBaz must be defined in classpath (as a '.' in this case)? I don't think we do since the program class is in the current directory
Ikpefua wrote: Hello Faraz, the reason why option B is wrong is explained in the K & B Book, go to chapter 10, page 798 READ CAREFULLY the first paragraph, it says quote: "A very common situation occurs in which java or javac complains that it can't find a class file, and yet you can see that the file is IN the current directory! When searching for class files, the java and javac commands DONT search the current directory by default. -YOU MUST TELL THEM TO SEARCH THERE-. The way to tell java or javac to search in the current directory is to add a dot (.) to the classpath" .
B. java -classpath test FindBaz // MUST add a dot "."
C. java -classpath .:test FindBaz
F. java -classpath test:test/myApp:. FindBaz
That is why option B is wrong and options C and F are correct.
The second paragraph has a caption that makes you understand this better and it says quote: "REMEMBER That we're talking about .class files,
When you're telling javac which .java file to compile, javac looks in the current directory by default".
Hint:
Take note of the difference between a .class file(already compiled) and a .java file(not yet compiled).
you will see that in the case of a .java file, you do NOT need to use a dot "." since javac looks in the current directory by default.
Where-as in the case of a.class file, you NEED to use a dot "." since java does NOT look in the current directory by default.
And one more thing, REMEMBER that for option B to be correct you MUST also use the colon ":" just as specified in option C, you know this is used in the command line to separate directories.
I hope this helps.