LinuxCommandLibrary

javac

Compile Java source code to bytecode

TLDR

Compile a .java file

$ javac [path/to/file.java]
copy

Compile several .java files
$ javac [path/to/file1.java path/to/file2.java ...]
copy

Compile all .java files in current directory
$ javac [*.java]
copy

Compile a .java file and place the resulting class file in a specific directory
$ javac -d [path/to/directory] [path/to/file.java]
copy

SYNOPSIS

javac [options] sourcefile.java...

PARAMETERS

-classpath path, -cp path
    Specifies where to find user class files, overriding CLASSPATH environment variable

-d directory
    Specifies destination directory for compiled class files

-source version
    Sets source code language version (e.g., 8, 11, 17)

-target version
    Generates class files for specified VM version

-verbose
    Enables verbose output, showing compilation progress

-deprecation
    Shows locations of deprecated API uses

-g
    Generates all debugging information

-g:none
    Does not generate debugging information

-Xlint
    Enables all recommended warnings

-Xlint:suboption
    Enables specific category of warnings (e.g., unchecked, cast)

-Jflag
    Passes flag directly to the runtime system

-sourcepath path
    Specifies path to source files

-bootclasspath path
    Overrides location of bootstrap class files

-extdirs dirs
    Overrides extension directories

DESCRIPTION

javac is the primary command-line compiler for the Java programming language, included in the Java Development Kit (JDK). It reads one or more .java source files containing Java class definitions and compiles them into platform-independent bytecode stored in .class files. These bytecode files are executed by the Java Virtual Machine (JVM) via the java command.

The compiler performs syntax analysis, type checking, flow analysis, and desugaring of syntactic constructs. It supports annotation processing, modular compilation (since Java 9), and generates metadata for debugging and optimization. Javac handles dependencies automatically via classpath scanning but requires explicit specification for external libraries.

Key features include support for generics, lambdas, modules, and records (Java 14+). Output can be directed to custom directories, with options for source/target compatibility, verbose logging, and warnings for deprecated APIs or unchecked operations. It's essential for building Java applications, libraries, and applets from source.

CAVEATS

Requires JDK installation (not just JRE); handles multiple files but needs all dependencies on classpath; fails on syntax/type errors without generating partial output by default; modular apps (Java 9+) require --module-path or --module-source-path.

EXAMPLE

javac -d bin -cp lib/* src/*.java
Compiles all .java files in src/ to bin/, using libraries in lib/.

NON-STANDARD OPTIONS

Use javac -X for extended options list; -Werror treats warnings as errors.

HISTORY

Introduced in JDK 1.0 (1996) by Sun Microsystems as the reference Java compiler. Evolved with language features like generics (JDK 5), modules (JDK 9), and preview features (JDK 12+). Maintained by Oracle in proprietary JDK and by OpenJDK community since 2006.

SEE ALSO

java(1), javap(1), jar(1), javadoc(1), jdb(1)

Copied to clipboard