Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java and javac commands don't search the current directory by default?

 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to implements and understand a paragraph in K&B which says: " When searching for class files , the java and javac commands don't search the current directory by default .

Thanks in advance.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javac and java commands will look for classes in the classpath, not in the current directory.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49445
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't specify a classpath, the JVM will "assume" your classpath is "." ie the current directory. If you do specify a classpath, that is what will be searched. There is a long thread which was finally resolved today, but it is mainly about classpath problems. Consider whether you are brave enough to read all that thread.

And since your other thread is about classpaths, I think it would be better to continue any discussion there, so I shall close this discussion.
 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please clear my another doubt regarding this : " How do we specify classpath?" i guess at command line with argument to >java " .

And when you say that java and javac looks for classes in current directory.. Does it mean .class file ?


 
fred rosenberger
lowercase baba
Bartender
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you set the classpath depends on what operating system you are on. On unix machines, you usually define it in your .profile or .cshrc file. On windows, you can rt-click on "My Computer", go to properties, advanced tab, and click on 'environment variable'. You can then set the PATH and CLASSPATH for either the system (i.e. everyone) or the user currently logged on.

Be EXTREMELY careful if you edit your PATH. If you mess it up, you can break all kinds of things.
And when you say that java and javac looks for classes in current directory.. Does it mean .class file ?

it looks for the .class files it needs in the current directory...directories are set up like a tree structure. you have a root directory, which then has sub-directories. each of those can have sub-directories, etc. at each level, there can also be files.

When you are compiling, you are generally in some directory. So, I may start up my command line tool, and go to "C:", then change directory (cd) to "c:/fred", then cd to "c:/fred/java". Then, if while in that directory, if I say "java MyClass", assuming your classpath was nothing (or nothing but a dot), the JVM would look for the file "MyClass" in the c:/fred/java directory, and ONLY look there. if the MyClass.java file was in c:/fred/java/class directory, the JVM would not know to look there.

 
Catherine austin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Fred..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic