Related Articles

Related Articles

Java.lang.Compiler Class in Java
  • Difficulty Level : Medium
  • Last Updated : 27 Apr, 2017

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 :

  • command() : 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  :
    compiler specific value
    Exception : 
    -> NullPointerException
    
  • compileClass() : java.lang.Compiler.compileClass() compiles the specified class.
    Syntax :



    public static boolean compileClass(Class c)
    Parameters : 
    c : class to be compiled.
    Return  :
     true if the compilation succeeded else, false
    Exception : 
    -> NullPointerException
    
  • enable() : java.lang.Compiler.enable() cause compiler to start operation.
    Syntax :

    public static void enable()
    Parameters : 
    ------
    Return  :
    void
    
  • disable() : java.lang.Compiler.disable() stops compiler to perform operations.
    Syntax :

    public static void disable()
    Parameters : 
    -------
    Return  :
    void
    
  • compileClasses() : 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  :
    true if the classes are compiled successfully 
    Exception : 
    -> NullPointerException
    
  • filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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()
            {
            }
        }
    }

    chevron_right

    
    

    Output :

    class NewClass$CompilerClass
    Value : null
    
    Is compilation successful ? : false
    
    Is compilation successful using str ? : false

    Note :
    lang.Compiler Class inherits others methods from 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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

    Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




    My Personal Notes arrow_drop_up
Recommended Articles
Page :