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

Creating, Compiling and Executing Java Programs

This section describes the process of Java program creation, compilation and execution. JDK 'javac' and 'java' commands are also described.

A Java application program is made of one or more classes and zero or more interfaces. One of the class must have the main() method as the execution starting point of the program. There are 3 steps involved in creating the classes and interfaces of a Java program, and running the program:

  • Creating Classes and Interfaces: Creating a text file for each class or interface that contains the definition of the class or interface written in Java language statements. Such a file is called source code file, which must have a file name identical to the name of the class or interface defined in the file, and an extension of ".java".
  • Compiling Classes and Interfaces: Converting a source code file into a bytecode file that contains the same definition of the class or interface written Java Virtual Machine (JVM) instructions. A bytecode file must have a file name identical to the name of the class or interface defined in the file, and an extension of ".class".
  • Executing a Java Program: Loading bytecode files into JVM, and executing its JVM instructions starting from the entry point of the specified class.

Creating a source code file can be done by a simple text editor, notepad, or a sophisticated Java development environment tool, like Eclipse, or Visual J++.

Compiling a source code file needs a Java compiler. The J2SDK package from Sun Microsystems contains a Java compiler, which can be invoked by command "javac". The "javac" command takes source code file names as command line arguments. For example, entering the following command in command window will invoke the "javac" compiler to compile the "Hello.java" source code file:

c:\jdk\bin\javac Hello.java

The "javac" command also has several command options available:

  • -d: Specify the directory where the bytecode files generated by the compiler should be placed, if you don't want to place them in the current directory.
  • -classpath: Specify the directories where to search for classes or interfaces definitions in bytecode format, if the classes or interfaces are used in source code file.
  • -sourcepatch: Specify the directories where to search for classes or interfaces definitions in source code format, if the classes or interfaces are used in source code file.

For example, the following command:

c:\jdk\javac -d .\cls -classpath .\lib;.\cls 
   -sourcepath .\src .\src\Test.java

invokes the "javac" compiler, and tells it to:

  • Compile the "Test.java" source code file in the ".\src" directory.
  • Place the bytecode file, "Test.class", in the ".\cls" directory.
  • Search ".\lib" and ".\cls" directories for the definitions in bytecode format of classes or interfaces that are used in "Test.java".
  • Search ".\src" directory for the definitions in source code format of classes or interfaces that are used in "Test.java".

During the compilation process, if the definition of a class or interface is used in the source code file, the compiler will use the following rules to search for the definition:

  • If the compiler could not find the class or interface in the class path and the source path, the compiler will produce a compilation error saying that "cannot resolve symbol" where the symbol is the name of the class or interface.
  • If the compiler find the class or interface only in the class path, the compiler will be happy to use it as it is.
  • If the compiler find the class or interface only in the source path, the compiler will compile it before using it.
  • If the compiler find the class or interface in the source path and in the class path, the compiler will compare those two files. If the source code file is newer than the bytecode file, the compiler will compile the source code file.

Executing a Java program needs a JVM. The J2SDK also contains a JVM, called HotSpot, which can be invoked by command "java". The "java" command takes the name of the starting class as the command argument. For example, the following command:

c:\jdk\bin\java -cp .\lib;.\cls;. Test 

invokes HotSpot to:

  • Load "Test" class into HotSpot.
  • Execute the main() method of the "Test" class.

The "-cp" option specifies the class path, which contains directories where the JVM will search for the bytecode files of the starting class and other classes when needed.

Table of Contents

 About This Book

 Installing JDK 1.4 on Windows 2000

 Installing JDK 1.5 on Windows XP

 Installing JDK 1.6 on Windows XP

Execution Process, Entry Point, Input and Output

Creating, Compiling and Executing Java Programs

 main() Method - Java Execution Entry Point

 Java Execution Console - "in", "out" and "err" Data Streams

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 References

 PDF Printing Version

Dr. Herong Yang, updated in 2008
Creating, Compiling and Executing Java Programs