"javac -d" - Specifying Output Directory

This section provides a tutorial example on how to use the 'javac -d' option to specify the output directory for the 'javac' tool to store class files generated by the 'javac' compiler.

If you are writing a more complex Java application, you need to write a number Java classes, which are required to store multiple class source files, one file per class.

When you have a large number class files, you should organize your Java classes into packages. The Java source files must be stored in sub directories with the directory path names matching package names.

For example, if a source file, Some.java, is defined in a package name as: "com.herongyang.util", it must be stored in a sub-directory with this pathname, ".\com\herongyang\util\Some.java".

By default, "javac" will output the class file in the same directory as the source file. But you can change this default behavior by using the "-d" option. It will make "javac" to output the class file into the specified directory.

To test "javac -d" option, I wrote the following Java source file: HelloPackaged.java, which defines a package Java class:

/* HelloPackaged.java
 * Copyright (c) 2015 HerongYang.com. All Rights Reserved.
 */
package com.herongyang.util;
public class HelloPackaged {
   public static void main(String[] a) {
      System.out.println("Hello world! - Packaged"); 	
   }
}

Test 1 - Storing HelloPackaged.java in the current directory, not in a sub-directory that matches the package name:

C:\herong>dir HelloPackaged.*
   243 HelloPackaged.java

C:\herong>javac HelloPackaged.java

C:\herong>dir HelloPackaged.*
   458 HelloPackaged.class
   263 HelloPackaged.java

C:\herong>java -classpath . HelloPackaged
Error: Could not find or load main class HelloPackaged
Caused by: java.lang.NoClassDefFoundError: 
   com/herong/util/HelloPackaged (wrong name: HelloPackaged)   

This proves that packaged classes can not be stored in any directory.

Test 2 - Storing HelloPackaged.java in a sub-directory that matches the class package name:

C:\herong>mkdir .\com
C:\herong>mkdir .\com\herong
C:\herong>mkdir .\com\herongyang\util

C:\herong>copy HelloPackaged.java .\com\herongyang\util

C:\herong>del PackagedHell.*

C:\herong>javac .\com\herongyang\util\HelloPackaged.java

C:\herong>dir .\com\herongyang\util\HelloPackaged.*
   458 HelloPackaged.class
   243 HelloPackaged.java

C:\herong>java -classpath . com.herongyang.util.HelloPackaged
Hello world! - Packaged

As you can see, the compiler outputs the class file in the same directory as the source file by default.

Test 3 - Outputing HelloPackaged.class to a new directory:

C:\herong>mkdir .\cls

C:\herong>javac -d .\cls .\com\herongyang\util\HelloPackaged.java

C:\herong>dir .\cls\com\herongyang\util\PackageHello.*
   458 HelloPackaged.class

C:\herong>java -classpath .\cls com.herongyang.util.HelloPackaged
Packaged: Hello world!

This time, the compiler outputs the class file under a different sub-directory: .\cls.

Last update: 2018.

Table of Contents

 About This Book

 Java Tools Terminology

 Java Tools Included in JDK

javac - The Java Program Compiler

 javac - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 "javac -classpath" - Specifying Class Path

 "javac -verbose" - Printing Compilation Details

 "javac -sourcepath" - Specifying Source Path

"javac -d" - Specifying Output Directory

 Two Types of "import" Statements

 "import" Statements Processed by "javac"

 "javac -g" - Controlling Debugging Information

 "javac --module" - Compiling Entire Module

 "javac -X" - Specifying Non-Standard Options

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 java - The Java Program Launcher

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 jmc - Java Mission Control

 javap - The Java Class File Disassembler

 keytool - Public Key Certificate Tool

 jarsigner - JAR File Signer

 jshell - Java Language Shell

 native2ascii - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 Full Version in PDF/EPUB