Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of writing a seemingly self-referential "-d . " in the command line interface

When compiling an application in the command line interface, I sometimes see a command written as follows:

javac -d . HelloWorld.java

I understand that:

  1. -d <directory> = Specify where to place generated class files
  2. . = the current folder

My 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
like image 791
beyondbrackets Avatar asked Dec 05 '25 06:12

beyondbrackets


1 Answers

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.

like image 160
Michel Billaud Avatar answered Dec 07 '25 21:12

Michel Billaud