javac
Compile Java source code to bytecode
TLDR
Compile a .java file
Compile several .java files
Compile all .java files in current directory
Compile a .java file and place the resulting class file in a specific directory
SYNOPSIS
javac [ options ] [ source files ] [ @listfiles ]
PARAMETERS
-classpath
Specify where to find user class files and dependent libraries. Overrides the CLASSPATH environment variable if set.
-d
Specify where to place generated class files.
-sourcepath
Specify where to find input source files.
-source
Specify the Java language version.
-target
Generate class files that target a specific JVM version.
-deprecation
Show the use of deprecated API elements.
-encoding
Specify the character encoding used by source files.
-g
Generate all debugging information.
-nowarn
Disable warning messages.
-verbose
Output messages about what the compiler is doing.
@
Read options and filenames from file.
DESCRIPTION
The javac command is the Java compiler. It compiles Java source code (.java files) into Java bytecode (.class files) that can be executed by the Java Virtual Machine (JVM). It's an essential tool for Java developers.
The compiler performs various checks during compilation, including syntax analysis, semantic analysis (type checking), and code generation. If any errors are found, javac will report them, preventing the generation of bytecode. The resulting .class files contain platform-independent bytecode, allowing Java applications to run on different operating systems that have a compatible JVM. javac supports a wide range of command-line options to control the compilation process, such as setting the classpath, specifying the target Java version, and generating debugging information. javac is part of the Java Development Kit (JDK).
CAVEATS
The classpath setting can be tricky. Ensure it includes all necessary JAR files and directories.
COMMON USAGE
A typical usage is: javac MyClass.java. This will compile MyClass.java and generate MyClass.class in the same directory. To specify output directory use -d: javac -d bin MyClass.java will generate the class in /bin directory.
ERROR HANDLING
javac provides detailed error messages, but they can sometimes be difficult to interpret. Understanding the error message is crucial for debugging.
HISTORY
javac was developed as part of the Java Development Kit (JDK) by Sun Microsystems (later acquired by Oracle). It has evolved with each new Java version to support new language features and improve performance. It's fundamental for building Java applications.