• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Chapter 10 K&B Self Test Question 11 Help

 
James David
Greenhorn
Posts: 4
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I am confused about this question.

Given the following directory structure:
test-|
|- GetJar.java
|
|- myApp-|
|-Foo.java
And given the contents of GetJar.java and Foo.java:


If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar
located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

Answer is given: A is correct.

My question is why C is wrong. The answer says C is wrong because the -classpath MyJar. in the java invocation does not include the test directory. Why javac -classpath MyJar.jar GetJar.java is right? MayJar.jar doesn't include the test directory. what is the differences between javac -classpath and java -classpath? what is "java -classpath" meaning?
 
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
When you run a javac command you specify a filename which needs to be compiled. When you provide the filename the compiler would look for that file in the current directory. But when you run the java command, you provide a class name and the class is only searched on the classpath. By default java command searches for classes in the current directory but if you provide a classpath and if it doesn't contain the current directory then java command will not look for classes in the current directory...
 
James David
Greenhorn
Posts: 4
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic