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 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 "" source code file:


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

For example, the following command:

C:\>\progra~1\java\jdk1.8.0\bin\javac -d .\cls 
   -classpath .\lib;.\cls -sourcepath .\src .\src\

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

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:

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:\herong>\progra~1\java\jdk1.8.0\bin\java -cp .\lib;.\cls;. Test

invokes HotSpot to:

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.

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

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

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 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

 Outdated Tutorials


 PDF Printing Version