Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compiling with javac and -cp

 
Jon Greenwood
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

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
PATH %JAVA_HOME%\bin;

my HelloWorld.java src file resides in C:\java

I can correctly compile from the working directory
C:\java>javac HelloWorld.java

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

What is the correct syntax if I try to compile from the previous parent directory?
C:>
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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

C:\>javac -cp . java\HelloWorld.java

OR

C:\>javac java\HelloWorld.java

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-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\HelloWorld.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\HelloWorld.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
Bartender
Pie
Posts: 5590
55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\HelloWorld.java

[edit]

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\HelloWorld.java

It's easier to just create a batch file in your project folder and do the following:
javac -cp src -d bin com\example\HelloWorld.java
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And better rename the folder java to say "source" to avoid confusion
 
Jon Greenwood
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic