javac Command in Linux



The javac command is a cross-platform utility used to compile Java programs. It reads Java source code (typically from .java files) and generates compiled class files with the ".class" extension. After this, the Java Virtual Machine (JVM) can execute the compiled file to produce the desired output.

Table of Contents

Here is a comprehensive guide to the options available with the javac command −

What is javac Command?

The javac command stands for Java Compiler. This command compiles Java source files into bytecode files. Once compiled, it can be executed by the Java Virtual Machine (JVM).

Syntax of javac Command

You can run the javac command in Linux, by using the following basic syntax −

javac [options] [sourcefiles-or-classnames]

Here, options represent the optional flags that show how the compiler behaves. The [sourcefiles-or-classnames] represent the source files or class names that you want to compile.

Options javac Command

The javac command accepts a wide range of options, among them the commonly used options are listed in the following table along with their description −

Option Description
-d <directory> It specifies the destination directory for the compiled Java files. If you do not specify this option, the .class files are placed in the same directory as the source files.
-classpath <path> | -cp <path> It specifies the classpath (a list of directories and JAR files) to search for referenced classes.
-source <version> It specifies the source code version compatibility. For instance, if you specify -source 1.8, it makes the code compatible with Java 8 source syntax.
-target <version> It specifies the bytecode version to be generated. It makes the compiled class compatible with older Java versions.
-verbose It shows detailed information about the compilation process.
-Xlint It enables all recommended compiler warnings. Or you can specify a category to enable a specific warning.
-g It generates debugging information, including local variable details, with only line numbers and source file information included by default.
-J<option> It passes arguments directly to the JVM running javac.
-encoding <encoding> It specifies the character encoding used by the source files (e.g., -encoding UTF-8).
-deprecation It shows warnings for the deprecated APIs.
-proc:none It disables annotation processing.
-proc:only It only runs annotation processors without compiling the code.
-s <directory> It specifies the directory where generated source files should be placed.
-sourcepath <path> It specifies the path of additional source files.
-bootclasspath <path> It overrides the bootstrap class files path/location.
-extdirs <dirs> It specifies directories containing extension classes.
-Werror It treats warnings as errors. As a result, compilation fails if any warnings are found.
-nowarn It disables compiler warnings.
-help It shows a list of available javac options.
-version It returns the installed version of the Java compiler.

To learn more about javac options, use the javac --help command or refer to its official manual page.

javac Manual Page

You can access the javac manual page by running the following command from the terminal −

man javac 
javac Manual Page1

Alternatively, you can run the javac command with the -help option to get a concise output −

javac -help
javac Manual Page2

How do you check if Javac is installed?

You can check the existence of the javac command in Linux by running the following command −

javac -version

This command returns the javac version, if it is installed on your system −

How do you check if Javac is installed in Linux?

However, if javac is not installed on your system, youll encounter a command not found error. In such cases you can run one of the following commands to install javac on your system −

#for Ubuntu or Debian
sudo apt install default-jdk
#for RHEL/CentOS/Fedora
sudo yum install java-11-openjdk
#For Arch Linux
sudo pacman -S jdk-openjdk

Java Development Kit (JDK) includes the Java compiler. So installing jdk will automatically install javac on your Linux system.

Examples of javac Command

Lets go through the following step-by-step to learn how the javac command works −

  • Compiling a Java Program Using javac
  • Compiling Multiple Java Files Using javac
  • Compiling All Java Files Using javac

Compiling a Java Program Using javac

In this example, we will create a Java program and compile it using the javac command. After that, we will run the compiled Java program using the java command −

Step 1: Create a Java File

Lets create a simple Java file in any editor like Nano −

nano javaExample.java

Now paste the following code into this file to print a greeting message on the console. Save the file and exit it −

public class ExampleClass{
   public static void main(String[] args) {
      System.out.println("Hi Geeks, Welcome to Tutorialspoint!");
   }
}

Step 2: Compile a Java File Using javac

Now use the javac command to compile the javaExample.java −

javac javaExample.java

This command will create a compiled file −

Compile a Java File Using javac

Step 3: Run the Java File

Now run the compiled Java file using the java command, as shown below −

java javaExample.class

You will get the desired output, as follows −

Run the Java File in javac

Compiling Multiple Java Files Using javac

We can compile multiple Java files with the javac command using a space-separated syntax −

javac example1.java example2.java example3.java

This command compiles example1, example2, and example3 files.

Compiling All Java Files Using javac

You can use the * wildcard with the javac command to compile all Java files in Linux. Here is a simple example −

javac *.java

This command will compile all the files in the current working directory that have a .java extension.

Conclusion

javac is a command-line utility in Linux that compiles Java source code into bytecode. This compiled code can be then executed by the JVM. The javac command supports several options to customize the compilation process, such as specifying the destination directory, classpath, and version compatibility. In Linux, users can easily use this command through the terminal for compiling single or multiple Java files efficiently.

In this tutorial, we covered the basic syntax of the javac command, explored its key options, demonstrated its usage in Linux, and provided examples to illustrate how to compile Java programs effectively.

Advertisements