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

Compiling Hello.java - My First Java Program

This section provides a tutorial example on how to compile a Java program using the 'javac' tool without any options.

To test the compiler, I wrote my first Java program with a text editor:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!"); 	
   }
}

Then I saved this program with the file Hello.java in a working directory C:\herong

Here is what I did in a command window to compile Hello.java with the 'javac' Java compiler tool. The output of the compilation is a Java class file called Hello.class:

C:\>cd \herong

C:\herong>\j2sdk1.5.0\bin\javac Hello.java

C:\herong>dir Hello.*
   416 Hello.class
   116 Hello.java

As you can see from this tutorial, the simplest way to use the 'javac' Java compiler is to:

  • Open a command line window.
  • Change the current directory to the directory with the Java program is stored.
  • Run the 'javac' Java compiler tool with the full path name \j2sdk1.5.0\bin\javac.exe.
  • The compilation output file is a class file, stored in the current directory.

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
Compiling Hello.java - My First Java Program