• Post Reply Bookmark Topic Watch Topic
  • New Topic

classpath and 'current directory'  RSS feed

 
Richard Broersma
Ranch Hand
Posts: 63
Firefox Browser Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading the SCJP book by Sierra and Bates. On Page 818 in chapter 10 on question 11, there is a problem regarding classpaths.

The correct answer is shown to be - and verified to be:

A. javac -classpath MyJar.jar GetJar.java
java GetJar


My concern revolves around this syntax:



Why is java finding the GetJar class in the current directory, when by default neither java or javac should look in the current directory for classes?

At least, this is my understanding of the paragraph found on page 798:

A very common situation occurs in which java or javac complains that it can't find a class file, and yet you see that the file is IN the current directory! When searching for class files, the java and javac commands don't search the current directory by default. You must tell them to search there. ...


Is there some facet to java[c] and classpaths that I'm misunderstanding?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Broersma wrote:Why is java finding the GetJar class in the current directory, when by default neither java or javac should look in the current directory for classes?

Since Java 5, java and javac do look in the current directory for class files by default (if you don't use the -cp or -classpath option), but only if you do not have the CLASSPATH environment variable set.

If you have CLASSPATH set or you use the -cp or -classpath option, you must explicitly add "." to the classpath to indicate that it should search the current directory.

For which version of Java is the book that you have; is it an older version? If not, then that sounds like an error, or at least an inaccuracy in the book.
 
Richard Broersma
Ranch Hand
Posts: 63
Firefox Browser Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:If you have CLASSPATH set or you use the -cp or -classpath option, you must explicitly add "." to the classpath to indicate that it should search the current directory.
Ah. That explains what I'm seeing.
Jesper de Jong wrote:For which version of Java is the book that you have; is it an older version?
The book is the version 6 study guide (Exam 310-065).
Jesper de Jong wrote:If not, then that sounds like an error, or at least an inaccuracy in the book.
I've reread the section describing the order that java and javac search class paths. But I don't see it where it describes using the current path by default. However, I can't be certain that this point isn't mentioned some where else.

Thanks for clarifying this for me.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!