It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes compiling with -d option Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "compiling with -d option" Watch "compiling with -d option" New topic
Author

compiling with -d option

Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

I understand that -d option is used to tell compiler in which directory to put the .class file. I consider the example from SCJP Sun Certified Programmer for Java 6 Study Guide by Katherine Sierra and Bert Bates (K&B) Page number 793. In this example, there is a command "javac -d ../classes com/wickedlysmart/MyClass.java" and the current director is myProject/source. my question is if we change the current directory to myProject/source/com then can we use three dots after -d option like as
"javac -d .../classes com/wickedlysmart/MyCass.java" to put class file in classes directory or there is any other way to do it.

here is the example:

package com.wickedlysmart; // change to package wickedlysmart
public class MyClass{ }

myProject
|
| ---source|-------com*|--------wickedlysmart|-----MyClass.java
|
|------classes|


Matloob
OCPJP 1.6
OCPWCD in progress.....
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

To go two directories up you'd use: javac -d ../../classes com/wickedlysmart/MyCass.java.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Jack Tol wrote:To go two directories up you'd use: javac -d ../../classes com/wickedlysmart/MyCass.java.


Hello Jack, what you have said is to go ONE directory up and NOT two directories up because the first "." dot refers to the current directory, to go two directories up, you need one more dot.


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Ikpefua Jacob-Obinyan wrote:
Jack Tol wrote:To go two directories up you'd use: javac -d ../../classes com/wickedlysmart/MyCass.java.


Hello Jack, what you have said is to go ONE directory up and NOT two directories up because the first "." dot refers to the current directory, to go two directories up, you need one more dot.


Hi Ikpefua,

How do you mean?

When the target destination is: myProject/classes
And current path is: myProject/source/com

.. would take you to the path: myProject/source
../.. would take you to the path myProject
and ../../classes would take you to the path myProject/classes, the destination directory.

Please do note that my previous reply was: twodots/twodots/classes.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Thanks Jack,
finally I was able to compile the class and put it in the destination directory EXACTLY as specified in Matlloob Hussain's description, with the following invocation:

javac -d ..\..\classes .\.\.\wickedlysmart\MyClass.java

P.S: I used back slash (\) because my system is Microsofts windows 7.

Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Ikpefua Jacob-Obinyan wrote:
javac -d ..\..\classes .\.\.\wickedlysmart\MyClass.java



Hi Ikpefua,

You used .\.\.\wickedlysmart\MyClass.jave to get java file. Could you please explain the use of .\ three times.
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Matloob Hussain wrote:
Ikpefua Jacob-Obinyan wrote:
javac -d ..\..\classes .\.\.\wickedlysmart\MyClass.java



Hi Ikpefua,

You used .\.\.\wickedlysmart\MyClass.jave to get java file. Could you please explain the use of .\ three times.


I'm not exactly sure why the .\.\.\ is used, but the following works for me in your situation:
javac -d ../../classes/ wickedlysmart/MyClass.java

../../classes gets you to myProject/classes as explained in my previous reply, and you can select class MyClass.java for compilation with wickedlysmart/MyClass.java, seeing as your current working directory is myProject/source/com.

My first reply contained an error in selecting MyClass.java for compilation (com/wickedlysmart/MyClass.java,) my apologies for this. This reply was incorrect because your current working directory is myProject/source/com. From this folder you get to MyClass.java with just wickedlysmart/MyClass.java.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello Matloob,
the reason why I used .\ three times was because I thought... ok since the sintax that Jack specified javac -d ..\..\classes takes us back to start from the myProject directory, why not start the .java file search from the myProject directory, I did that and it worked, that is NOT to say its the most adecuate, it is NOT, and as a matter of fact in java the lesser sintax you use the better.

@Jack your sintax is correct and better than mine. ...Now for the exams I ALMOST certain that you will NOT get a 'wicked' question like this, however on-the-job this makes us 'better equiped' towards managing file systems vs .java and .class files.
Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Hi Ikpefua,

Thank you for reply, I was not sure about .\.\.\ now I got it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compiling with -d option
 
Similar Threads
Clarification on classpaths
knb page# 793 doubt.
K&B 6 book Page 792
please help me for this package question
classpath and javac from command line