Javac Command - Compile Java Source Files | Online Free DevTools by Hexmos

Compile Java source files with the Javac command. Learn to use Javac for compiling single or multiple files, setting classpaths, and enabling warnings.

Javac Command Reference

The javac command is the Java compiler. It translates Java source code (.java files) into Java bytecode (.class files) that can be executed by the Java Virtual Machine (JVM).

Compile Java Source Files

This section provides common usage examples for the javac command.

Basic Compilation

# To compile a simple source file:
javac HelloWorld.java

Compiling Multiple Files

# To compile several source files:
javac *.java

Specifying Output Directory

# To specify another destination directory for compiled class files:
javac -d build HelloWorld.java

Using Source Path

# To use another directory for source dependencies:
javac -sourcepath src/dependencies/java Main.java

Setting Classpath

# To define where compiled dependencies (libraries) should be searched:
javac -classpath lib/commons-cli-1.4.jar:lib/log4j-1.2.12.jar HelloWorld.java

Treating Warnings as Errors

# To consider warnings as errors:
javac -Werror NoWarning.java

Specifying Java Source Version

# To compile Java 7 code:
javac -source 1.7 Java7.java

Verbose Compilation

# To make the compiler more verbose, showing detailed output:
javac -verbose *.java

Displaying Deprecated API Usage

# To display usage of deprecated APIs:
javac -deprecation App.java

Including Debugging Information

# To include debugging information in class files:
javac -g HelloWorld.java

Displaying Javac Version

# To display the version of the Java compiler:
javac -version

Getting Help

# To display the help message and available options:
javac -help

Understanding Javac Options

The javac command offers numerous options to control the compilation process. Key options include:

  • -d <directory>: Specifies the destination directory for compiled class files.
  • -sourcepath <path>: Specifies the search path for source files.
  • -classpath <path> or -cp <path>: Specifies the search path for compiled class files and other resources.
  • -Werror: Treats all warnings as errors, causing the compilation to fail if any warnings are generated.
  • -source <version>: Specifies the version of the source code.
  • -verbose: Outputs additional information about the compilation process.
  • -deprecation: Reports on the use of deprecated APIs.
  • -g: Enables the generation of debugging information.

External Resources