Skip to content
Related Articles
Open in App
Not now

Related Articles

Compiler Class in Java

Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 09 Feb, 2022
Improve Article
Save Article
Like Article

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). 


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. 


public static boolean command(Object argument)


  • 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. 


public static boolean compileClass(Class c)


  • c: class to be compiled.

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”.


public static boolean compileClasses(String string)


  • str: name of the class to be compiled.

Return: It returns true if the classes are compiled successfully.

Exception: It throws NullPointerException.



// 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() :
        // class CompilerDemo
        Class c = geek.getClass();
        // 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);
            "\nIs compilation successful ? : " + check);
        String str = "CompilerClass";
        boolean check1 = Compiler.compileClasses(str);
            "\nIs compilation successful using str ? : "
            + check1);
        // Use of disable() :
    private static class CompilerClass {
        public CompilerClass() {}


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.

This article is contributed by Mohit Gupta_OMG. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article at See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!