When compiling an application in the command line interface, I sometimes see a command written as follows:
javac -d . HelloWorld.java
I understand that:
-d <directory> = Specify where to place generated class files. = the current folderMy question is: what is the purpose of writing -d .?
It seems self-referential and completely redundant/unnecessary. I would expect simply the following, which to my knowledge has the same effect and is less verbose:
javac HelloWorld.java
Is there something that I am missing?
I have used symbolhound.com to search the web for this specific phrase, but could not find any explanation.
This page on the Oracle Java site does it, for instance:
javac -d . XorInputStream.java javac -d . XorOutputStream.java javac -d . XorSocket.java javac -d . XorServerSocket.java javac -d . XorServerSocketFactory.java javac -d . XorClientSocketFactory.java javac -d . Hello.java javac -d . HelloClient.java javac -d . HelloImpl.java
WHEN THE SOURCE DIR IS NOT THE CURRENT DIR
Suppose you have a Hello.java file in /tmp/src
When you are in /tmp, compiling with
javac -d . src/Hello.java
puts the class file in the current directory, so it is /tmp/Hello.class.
Without the option, it goes to the same directory as the source file, that is /tmp/src/Hello.class.
MOREOVER there is a difference when packages are involved. Compiling this code
package Foo;
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
from the /tmp/src directory with the -d . option builds a subdirectory for the package
/tmp/src
├── Foo
│ └── Hello.class
└── Hello.java
whereas javac Hello.java leaves the class file in the current directory
$ tree
.
└── Hello.java
0 directories, 1 file
$ javac Hello.java
$ tree
.
├── Hello.class
└── Hello.java
0 directories, 2 files
No animal was hurt during the test with javac 1.8.0_101.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With