File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java and javac commands don't search the current directory by default? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java and javac commands don Watch "java and javac commands don New topic
Author

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

Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
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

Joined: May 13, 2002
Posts: 180
The javac and java commands will look for classes in the classpath, not in the current directory.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38478
    
  23
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

Joined: Jul 20, 2011
Posts: 43
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

Joined: Oct 02, 2003
Posts: 11256
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Catherine austin
Ranch Hand

Joined: Jul 20, 2011
Posts: 43
Thanks a lot Fred..
 
jQuery in Action, 2nd edition
 
subject: java and javac commands don't search the current directory by default?