Java is a language that requires compilation, but it differs significantly from conventional compilation as the source file is translated into byte code. Javac is the most widely accepted Java compiler.
One of the key modules for creating Java applications is the JDK, or Java Development Kit, introduced by Oracle. Javac is the primary sub-module of this module.
Working of the Compiler in Java?
In general, a compiler transforms the source code into target code. Typically, this target code is machine language, and it varies from one machine to another. However, the Java compiler transforms the source code into byte code, serving as transitional code.
This form of code is machine-independent, implying that you can run it on any platform that provides 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. After that, run the compiler by typing the file name with Javac.
The 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:
javac myname.java
If ‘myname‘ points to other classes in a separate Java file but is 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 the option -d for this purpose. For example:
javac -d <target directory> myname_file.java
Conclusion
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.