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 Compiling with javac and -cp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compiling with javac and -cp" Watch "Compiling with javac and -cp" New topic

Compiling with javac and -cp

Jon Greenwood

Joined: Mar 13, 2011
Posts: 15

javac resides in C:\Program Files\Java\jdk1.6.0_24\bin

I have the following Environment Variables
JAVA_HOME C:\Program Files\Java\jdk1.6.0_24

my src file resides in C:\java

I can correctly compile from the working directory

Which I think actually defaults to
C:\java>javac -cp .
where the period[.] is the current directory

What is the correct syntax if I try to compile from the previous parent directory?
Note: I specifically want to use the -cp argument and not set CLASSPATH=

Presumably I need to navigate from the javac.exe?

Thanks in advance
Amit N Chudasama

Joined: Nov 08, 2011
Posts: 2

Probably you can do it like below ... but at first WHY do you want this ?

C:\>javac -cp . java\


C:\>javac java\

John Jai

Joined: May 31, 2011
Posts: 1776
-cp option is used to find the .class files (or jars) that will be referred in the source files (.java) files.

So as Amit said this is the way to compile if source file is in different folder

C:\>javac java\

And below command is just looking for referenced class files in the current directory, but takes the source path from the java folder.
And hence it's very much equivalent to the above command.

C:\>javac -cp . java\

And since the -cp points to the .class files, you can use it in the java command without specifying its parent folder like below.

java -cp .\java HelloWorld

In the above command, the java tool looks for HelloWorld.class in the specified java folder...
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4215

It's pretty simple. Just use the -sourcepath switch to specify where to find the source files.

For example: javac -sourcepath C:\Projects\MyProject\src -d C:\Projects\MyProject\bin com\example\


Actually, now that I investigated a bit more, you can't do this. -sourcepath only works for classes referenced by the source you're trying to compile. You still need to name the main class explicitly:
javac -sourcepath C:\Projects\MyProject\src -d C:\Projects\MyProject\bin C:\Projects\MyProject\src\com\example\

It's easier to just create a batch file in your project folder and do the following:
javac -cp src -d bin com\example\

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
John Jai

Joined: May 31, 2011
Posts: 1776
And better rename the folder java to say "source" to avoid confusion
Jon Greenwood

Joined: Mar 13, 2011
Posts: 15
Thanks for your help
I agree. Here's the link:
subject: Compiling with javac and -cp
It's not a secret anymore!