File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inconsistent behaviour of -classpath between java & javac Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inconsistent behaviour of -classpath between java & javac" Watch "Inconsistent behaviour of -classpath between java & javac" New topic

Inconsistent behaviour of -classpath between java & javac

Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 91
I came across this issue whilst attempting a practice question in Sierra & Bates p818 q12.

Suppose I have and a directory testdir in the current directory.

I can compile HelloWorld, even if I supply a command line classpath that does not include the current directory:

javac -classpath testdir

So, given that the file I want to compile was supplied as an argument, javac can find it, even though the supplied classpath didn't tell it to look in the current directory. (Were to use some other class, also in this same directory, the compile would fail.)

Now I try to launch HelloWorld.class, using the same classpath:

java -classpath testdir HelloWorld

I get an error, "Error: Could not find or load main class in HelloWorld"
I have to include the current directory in the supplied classpath:

java -classpath testdir;. HelloWorld

Now it works.

So, am I right in thinking that the way java & javac use the -classpath argument is slightly different, in that javac automatically adds . to it?

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15101

The classpath is only used to search class files - not source files. So, javac is not using the classpath to find your source file - it just looks in the current directory and finds it there.

If your HelloWorld class references other classes, then javac would use the classpath to find the class files for those classes.

Also, note that javac takes the file name of a source file, for example, as an argument, while java takes a class name (not a file name).

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

think of classpath as CLASS SEARCH PATH. it contains path for searching of .class files and not .java files. by default javac will look in current directory for any source files or class files. if you have set classpath environment variable then the default path ( . ) is overridden(so that you have to include current directory in your classpath if you want the javac and java binaries to look in your current directory). just for additional information , corresponding to -classpath switch there is another option -sourcepath and as you have guessed this is for the searching of source file path. this is useful for e.g. suppose you are compiling a source file , which requires, which are in c:/Demo dir. then for this the command will be javac -sourcepath c:/Demo this will compile all the 3 source files.
Richard Hayward
Ranch Hand

Joined: Feb 15, 2012
Posts: 91
Hi Jesper.

classpath is only used to search class files - not source files

Now you point this out, it's obvious! Thank you.

I now notice that javac also has a -sourcepath option. I'll have a play with that too.


I agree. Here's the link:
subject: Inconsistent behaviour of -classpath between java & javac
It's not a secret anymore!