How To Compile Java Source Code with Javac Tool?
javac is a tool used to compile Java applications or source code. javac reads class and interface definitions and compiles them into bytecode class files. These class files have *.class extension.
javac usage syntax is a bit complex according to generally Linux tools.
javac OPTIONS SOURCEFILES CLASSES ARGFILES
- OPTIONS are used to specify command line options provided by javac
- SOURCEFILES are used to specify one or more source files to be compiled
- CLASSES are used to specify one or more classes to be processed for annotations such as YourPackage.YourClass
- ARGFILES are used to specify one or more files that contains options and sources files
Show Javac Version
Java Development Kit(JDK) and Java Runtime Environment(JRE) can have different versions we target this will also affect the version of the javac. If we want to use specific Java programming language version we need to check the javac version with the
$ javac --version
Compile Java Source Code
We will start with a simple javac example where we will compile a single Java source code file. As we know Java source code files generally uses *.java extension. We will just provide the file name to the javac compiler which is my.java in this example.
We will use the following sample HelloWorld.java source code
/*FileName : "HelloWorld.java" Example Application" */
public static void main(String args)
$ javac HelloWorld.java
$ file HelloWorld.class
Compile Multiple Java Files
In the previous example, we have compiled a single Java file. But this is insufficient for big projects where we need to compile multiple Java files in a single command. We can specify multiple Java files to the javac in order to compile to the byte code. We will just append the file names by separating with spaces.
$ javac HelloWorld.java HiWorld.java IAmHere.Java
Compile All Java Files
We can also compile all java files in the specified directory we will just use asterisk and .java to specify files with java extension in the current directory.
$ javac *.java
We can also specify a different directory from the current working directory. In this example, we will compile all Java files residing in
$ javac /home/ismail/MyProject/*.java
Print Detailed Compile Log In Verbose Mode
During compilation, we may need to print detailed information like libraries, operations warnings etc. We can use the
-verbose option which will provide detailed debug information about the compile operation.
$ javac -verbose HelloWorld.java
Specify Java Files and Options From File
Up to now, we have provided the Java files and options from the interactive shell or command line. If there are a lot of detailed file names and options where we will run them over and over again we can use files to specify the Java file named and options. We will use a file named config which will provide Java files and javac option to the javac command. We will use
@ sign before config file.
Here is the config file content which is used in previous examples.
We will provide this config file with the
@ sign like below. Keep in mind that we can use a different name than config for this feature.
$ java @config
Specify The Destination Path
After given Java files are compiled into byte code the new files are stored in the current working directory. These file will have the same name with the source file and extension will be class. We can specify different output or destination path to put created byte code or binary files We will use
-d option and the destination path.
$ javac -d /home/ismail/binary HelloWorld.java
Specify Class Path
If we need to provide types required to compile our source file we can use the
-cp option and provide class path like below. If this option is not provided the CLASSPATH environment variable will be used.
$ javac -cp /bin/lib/MyProject HelloWorld.java
Specify The Module or Library Path
We can also use
-p option in order to specify modules location. But this option can be used after Java 9.
$ javac -p /bin/lib/MyProject HelloWorld.java