"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) 2005 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:

herong> dir HelloPackaged.*
   243 HelloPackaged.java

herong> javac HelloPackaged.java

herong> dir HelloPackaged.*
   458 HelloPackaged.class
   263 HelloPackaged.java

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:

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

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

herong> del PackagedHell.*

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

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

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 - Outputting HelloPackaged.class to a new directory:

herong> mkdir .\cls

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

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

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.

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

 java - The Java Program Launcher

 jar - The JAR File Tool

 jlink - The JRE Linker

 jmod - The JMOD File Tool

 jimage - The JIMAGE File Tool

 jpackage - Binary Package Builder

 javadoc - The Java Document Generator

 jdeps - The Java Class Dependency Analyzer

 jdeprscan - The Java Deprecated API Scanner

 jdb - The Java Debugger

 jcmd - The JVM Diagnostic Tool

 jconsole - Java Monitoring and Management Console

 jstat - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jhsdb - The Java HotSpot Debugger

 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

 jrunscript - Script Code Shell

 Miscellaneous Tools

 native2ascii - Native-to-ASCII Encoding Converter

 JAB (Java Access Bridge) for Windows

 Archived Tutorials


 Full Version in PDF/EPUB