← 返回命令列表

Linux command

javac 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Compile a Java file

javac [File.java]

Compile multiple files

javac [File1.java] [File2.java]

Compile all Java files

javac *.java

Compile with classpath

javac -cp [lib/*:classes] [File.java]

Specify output directory

javac -d [out] [File.java]

Compile with specific

javac --release [11] [File.java]

Enable all warnings

javac -Xlint:all [File.java]

Compile with verbose output

javac -verbose [File.java]

说明

javac compiles Java source files (.java) into bytecode class files (.class) that run on the Java Virtual Machine. It performs syntax and type checking, optimizes code, and generates platform-independent bytecode. The compiler uses the classpath to locate referenced classes and libraries. For modular projects (Java 9+), the module path specifies module locations. The --release flag ensures compatibility with a specific Java version for both compilation and available APIs. When compiling many files, use @argfiles to avoid command-line length limits. Each line in the argfile can contain a source file or option.

参数

-d _DIR_
Output directory for compiled class files.
-cp, -classpath _PATH_
Classpath for finding user class files and libraries.
--module-path _PATH_
Module path for finding application modules.
-sourcepath _PATH_
Path for finding source files.
--release _VERSION_
Compile for specific Java SE release.
-source _VERSION_
Source code compatibility version.
-target _VERSION_
Generate class files for specific VM version.
-Xlint:_WARNINGS_
Enable warnings (all, deprecation, unchecked, etc.).
-g
Include debugging information.
-verbose
Output messages about what the compiler is doing.
-deprecation
Show description of deprecated API usage.
-encoding _ENCODING_
Source file character encoding.
-h _DIR_
Generate native header files for JNI.
@_FILE_
Read options and filenames from file.

FAQ

What is the javac command used for?

javac compiles Java source files (.java) into bytecode class files (.class) that run on the Java Virtual Machine. It performs syntax and type checking, optimizes code, and generates platform-independent bytecode. The compiler uses the classpath to locate referenced classes and libraries. For modular projects (Java 9+), the module path specifies module locations. The --release flag ensures compatibility with a specific Java version for both compilation and available APIs. When compiling many files, use @argfiles to avoid command-line length limits. Each line in the argfile can contain a source file or option.

How do I run a basic javac example?

Run `javac [File.java]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -d _DIR_ do in javac?

Output directory for compiled class files.