Compiler Class provides support and related services to Java code to Native Code. Native code is a form of code that can be said to run in a virtual machine (for example, [JVM]Java Virtual Machine).
Declaration:
public final class Compiler extends Object
Methods of Java Compiler Class
1. command()
The java.lang.Compiler.command() tests the argument type and performs some documented operations.
Syntax:
public static boolean command(Object argument)
Parameters:
- argument: needs to be compiler-specific.
Return: It returns the compiler-specific value.
Exception: It throws NullPointerException.
2. compileClass()
The java.lang.Compiler.compileClass() compiles the specified class.
Syntax:
public static boolean compileClass(Class c)
Parameters:
Return: It returns true if the compilation succeeded. Else, false.
Exception: It throws NullPointerException.
3. enable()
The java.lang.Compiler.enable() cause compiler to start operation.
Syntax :
public static void enable()
Return: It returns nothing.
4. disable()
The java.lang.Compiler.disable() stops compiler to perform operations.
Syntax :
public static void disable()
Return: It returns nothing.
5. compileClasses()
The java.lang.Compiler.compileClasses() compiles the classes having the name as string - "str".
Syntax:
public static boolean compileClasses(String string)
Parameters:
- str: name of the class to be compiled.
Return: It returns true if the classes are compiled successfully.
Exception: It throws NullPointerException.
Example:
Java
// Java Program illustrating the use
// of Compiler class Methods.
import java.lang.*;
public class NewClass {
public static void main(String[] args)
{
CompilerClass geek = new CompilerClass();
// Use of enable() :
Compiler.enable();
// class CompilerDemo
Class c = geek.getClass();
System.out.println(c);
// Use of command() :
Object g = Compiler.command("javac CompilerClass");
System.out.println("Value : " + g);
// Use of compileClass :
// Since it is not a subclass so there is no
// compiler for it
boolean check = Compiler.compileClass(c);
System.out.println(
"\nIs compilation successful ? : " + check);
String str = "CompilerClass";
boolean check1 = Compiler.compileClasses(str);
System.out.println(
"\nIs compilation successful using str ? : "
+ check1);
// Use of disable() :
Compiler.disable();
}
private static class CompilerClass {
public CompilerClass() {}
}
}
Output:
class NewClass$CompilerClass
Value : null
Is compilation successful ? : false
Is compilation successful using str ? : false
Note: The Compiler Class in Java inherits others methods from the Object class in Java.
Similar Reads
Java Class File A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know, a single Java programming language source file (or we can say .java file)
5 min read
10 Best Java Compilers in 2025 Java Compilers refers to a program that takes the text file work of a software developer and compiles it into a platform-independent Java file. The Java compilers mainly include the Java Programming language compilers (javac), the Eclipse compiler for Java(ECJ), the GNU, and Jikes. The Java Compiler
7 min read
Byte Code in Java Byte Code Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language. How is Byte Code generated? Compiler converts the source code or the Java program into the Byte
2 min read
Just In Time Compiler The JIT or Just-In-Time compiler is an essential part of the JRE (Java Runtime Environment), that is responsible for performance optimization of java based applications during run time. The compiler is one of the key aspects in deciding the performance of an application for both parties i.e. the end
3 min read
Java.lang.Class class in Java | Set 1 Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It
15+ min read
Coding Guidelines in Java Java is one of the most popular and widely used programming languages and platforms. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable, and secure. From desktop to web applications, scientific supercomputers to gaming cons
7 min read
Compile Time Polymorphism in Java Polymorphism in Java refers to an object's capacity to take several forms. Polymorphism allows us to perform the same action in multiple ways in Java. Polymorphism is divided into two types: Compile-time polymorphismRun time polymorphismNote: Run time polymorphism is implemented through Method overr
7 min read
Why Java is Called a Compiler Interpreter Language? Java is both a compiled and interpreted programming language. Because it employs both compilation and interpretation to run code, it is known as a "compiler-interpreter language." In order for the Java Virtual Machine to understand the Java code, it must first be compiled into an intermediate format
4 min read
Java 11 - Features and Comparison Every 6 months, Oracle releases new Java versions. The September Java 11 version is already making a lot of buzz in the computer science world even before it was released by declaring that from now on Java is gonna be paid for commercial use. The following articles will show important features, enha
5 min read
Interesting Facts About Java Java: A general-purpose, high-level programming language. It is developed by Sun Microsystems. It was developed by a mini team of engineers which is known as the Green Team. They initiated this language in 1991. Here are some interesting facts about Java: The initial name of java was "Oak". It was c
2 min read