File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Study Guide 310-065: Classpath Confusion 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 "Study Guide 310-065: Classpath Confusion" Watch "Study Guide 310-065: Classpath Confusion" New topic
Author

Study Guide 310-065: Classpath Confusion

George Impagliazo
Ranch Hand

Joined: Sep 25, 2010
Posts: 41
Hi. I took a quiz question in a book concerning the use of classpath
and JARs. The answers given by the book seem inconsistent with the
rules and with other answers in the same question. I think there is
a special rules for JARs and class paths that I missed?

Okay.

The current directory is "test" which has MyJar.jar in it.

test -|
MyJar.jar
MyProgram.java
|
| ____ myApp ( a subdirectory )|

|__ Foo.java ( in subdirectory myApp )

MyProgram.java calls Foo.java. It is dependent on Foo.java and must
find it to run

The book said that these two commands will compile and run MyProgram.java if the current directory is "test"

javac -classpath MyJar.jar MyProgram.java
java MyProgram

1. I don't understand how "java" finds the MyProgram class since the book says that the JVM will not search the current directory for class files unless it is listed in the classpath.

2. I don't understand how javaC is finding MyJar.jar. In an earlier example the book stated the compiler will not search the current
directory if the classpath is relative and starts with the current directory. Is the rule different for JARs? WIll the compiler search
the current directory for JAR if the JAR is listed in a relative ( and correct for the current dir ) classpath?

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The default class path is the current directory but setting the classpath implicitly overrides it, so I agree with 1.
2. This is different. 1. is about looking for classes using the classpath. 2. is about setting paths in the classpath. "javac -classpath MyJar.jar ..." is equivalent to "javac -classpath ./MyJar.jar..."


[My Blog]
All roads lead to JavaRanch
George Impagliazo
Ranch Hand

Joined: Sep 25, 2010
Posts: 41
Christophe Verré wrote:The default class path is the current directory but setting the classpath implicitly overrides it, so I agree with 1.
2. This is different. 1. is about looking for classes using the classpath. 2. is about setting paths in the classpath. "javac -classpath MyJar.jar ..." is equivalent to "javac -classpath ./MyJar.jar..."


That clears much up, thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Study Guide 310-065: Classpath Confusion
 
Similar Threads
K&B doubt regarding question in Development chapter
SCJP chapter 10 : Development
Confusion over java classpath
Chapter 10 Question 11 of SCJP K and B
Doubt in K&B Self Test from Last Chapter