How To Compile Java Source Code with Javac Tool? – POFTUT

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.

Syntax

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 contain 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  --version.

$ javac --version
Show Javac Version
Show 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 use *.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 file. We will try to compile this Java source file with the javac command line tool.

/*FileName : "HelloWorld.java" Example Application" */

class HelloWorld 
{ 
    public static void main(String args[]) 
    { 
        System.out.println("Hello, World"); 
    } 
}

Below we will provides the java source file name HelloWorld.java which will compile and create the compiled byte code file named HelloWorld.class. As you can see the new extension is .class which is a compiled Java class file.

$ 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 them 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 /home/ismail/MyProject.

$ 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
Print Detailed Compile Log In Verbose Mode
Print Detailed Compile Log In Verbose Mode

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 the config file.

LEARN MORE  Java JDK vs JRE What Are Differences and Similarities?

Here is the config file content which is used in previous examples.

HelloWorld.java

-verbose

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 JavaFiles and Options From File
Specify JavaFiles and Options From File

Specify The Destination Path

After given Java files are compiled into byte code the new files are stored in the current working directory. These files will have the same name as 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 classpath 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

Leave a Comment