Java is a language that needs compilation. But it is quite different from conventional compiling as the source file is translated to byte code. Javac is the most accepted Java compiler.
One of the key modules to create Java applications is JDK or Java Development Kit. It was introduced by Oracle and Javac is the primary sub-module of this module.
Also Read: Top 10 Java Libraries You Must Know in 2021
Working of the Compiler in Java
In general, a compiler changes the source code to target code. Typically, this target code is machine language, and it varies from machine to machine. However, the Java compiler changes the source code to byte code that serves as a transitional code.
This form of code is machine-independent, which implies that you can run it on any platform providing a virtual machine for Java or JVM. JVM operates like an interpreter.
Program Compilation Using Javac
To compile a Java program, you need to go to the command prompt and run the compiler by typing the file name with Javac. Javac command comes with multiple options like cross-compilation.
For instance, let’s say you named your Java program file as ‘myname.java’. You can compile this by using Javac as follows:
If myname points to some other classes in a separate Java file but saved in the related folder, javac will also compile that file. If you want, you can specify several file names explicitly. For example,
javac myname.java another_file.java oneMore_file.java
To define the target class directory: For large projects, it is recommended to maintain both the source and target files in separate directories. You can use option –d for this purpose. For example,
javac –d <target directory> myname_file.java
Javac is the most reliable compiler if you want to compile a Java program. Java compilers are ever-evolving over time, with plenty of new features being added.