Skip to content

Compiling and running Java programs (javac, java)

Java uses a two‑step process: compilation to bytecode, then interpretation/JIT execution.

The Java compiler (javac) translates .java source files into .class bytecode files.

Basic usage:

Terminal window
javac MyProgram.java

If the program has multiple classes, you can compile them all:

Terminal window
javac *.java

Common options:

  • -d <directory> – specify where to place generated .class files.
  • -classpath / -cp – specify where to find required libraries.

The Java launcher (java) starts the JVM and executes the bytecode.

Basic usage:

Terminal window
java MyProgram

You pass the class name (without .class). The JVM looks for a public static void main(String[] args) method in that class.

To run a program that resides in a package:

Terminal window
java com.example.MyProgram

You can pass command‑line arguments:

Terminal window
java MyProgram arg1 arg2

Inside the program, args contains ["arg1", "arg2"].

The classpath tells javac and java where to find user‑defined classes and libraries. It can be set via the -cp option or the CLASSPATH environment variable.

Example:

Terminal window
javac -cp .:lib/* MyProgram.java
java -cp .:lib/* MyProgram

JAR (Java Archive) files bundle multiple classes and resources. You can run an executable JAR with:

Terminal window
java -jar myapp.jar

This requires the JAR’s manifest to specify the main class.