Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

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) 2006 by Dr. Herong Yang, http://www.herongyang.com/
 */
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.*
   264 PackagedHello.java

C:\herong>javac PackagedHello.java

C:\herong>dir PackagedHell.*
   459 PackagedHello.class
   264 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
   459 PackagedHello.class
   264 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
   459 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.

Sections in This Chapter

'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

Dr. Herong Yang, updated in 2008
Option '-d' - Specifying Output Directory