File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Study Guide 310-065: Classpath Confusion

 
George Impagliazo
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..."
 
George Impagliazo
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic