Option '-d' - Specifying Output Directory

This chapter describes the Java compilation tool 'javac'. Topics include listing of 'javac' options, specifying class path with '-classpath', specifying source path with '-sourcpath', processing 'import' statements, generating debugging information with '-g'.

If you are writing a real Java application, you will organize your Java classes into packages. The Java source files must be stored in a directory with the path names match the package names. For example, if a source file, Some.java, is defined in a package name as: "com.herong.util", it must be stored in a directory named as: .\com\herong\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 files into the specified directory.

To test this option, I wrote the following Java source file: PackagedHello.java

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

Test 1 - Storing PackagedHello.java in the wrong directory:

C:\herong>dir PackagedHell.*
   263 PackagedHello.java

C:\herong>javac PackagedHello.java

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

C:\herong>java -cp . PackagedHello
Exception in thread "main" java.lang.NoClassDefFoundError: 
   PackagedHello (wrong name: com/herong/util/PackagedHello)

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

Test 2 - Storing PackagedHello.java in the right directory:

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

C:\herong>copy PackagedHello.java .\com\herong\util

C:\herong>del PackagedHell.*

C:\herong>javac .\com\herong\util\PackagedHello.java

C:\herong>dir .\com\herong\util
   458 PackagedHello.class
   263 PackagedHello.java

C:\herong>java -cp . com.herong.util.PackagedHello
Packaged: Hello world!

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

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

C:\herong>mkdir .\cls
C:\herong>javac -d .\cls .\com\herong\util\PackagedHello.java

C:\herong>dir .\cls\com\herong\util
   458 PackagedHello.class

C:\herong>java -cp .\cls com.herong.util.PackagedHello
Packaged: Hello world!

This time, the compiler outputs the class file under a directory path: .\cls.

Last update: 2015.

Table of Contents

 About This Book

 Java Tools Terminology

 Installing Java 8 on Windows

'javac' - The Java Program Compiler

 'javac' - Java Compilation Command and Options

 Compiling Hello.java - My First Java Program

 Option '-classpath' - Specifying Class Path

 Option '-sourcepath' - Specifying Source Path

Option '-d' - Specifying Output Directory

 Two Types of 'import' Statements

 'import' Statements Processed by 'javac'

 Option "-g" - Controlling Debugging Information

 'java' - The Java Program Launcher

 'jdb' - The Java Debugger

 'jconsole' - Java Monitoring and Management Console

 'jstat' - JVM Statistics Monitoring Tool

 JVM Troubleshooting Tools

 jvisualvm (Java VisualVM) - JVM Visual Tool

 'jar' - The JAR File Tool

 'javap' - The Java Class File Disassembler

 'keytool' - Public Key Certificate Tool

 'native2ascii' - Native-to-ASCII Encoding Converter

 Outdated Tutorials

 References

 PDF Printing Version