Java.lang Package Classes

Java Compiler - compileClass() Method



The java.lang.Compiler.compileClass() used to compile the specified class.

Syntax

public static boolean compileClass(Class<?> clazz)

Parameters

clazz Specify a class.

Return Value

Returns true if the compilation succeeded; false if the compilation failed or no compiler is available.

Exception

Throws NullPointerException, If clazz is null.

Example:

The example below shows how to use java.lang.Compiler.compileClass() method.

public class MyClass {
  public static void main(String[] args) {
    MyClass x = new MyClass();
    subMyClass y = new subMyClass();

    //printing class of x
    Class xcls = x.getClass(); 
    System.out.println(xcls);

    //printing class of y
    Class ycls = y.getClass(); 
    System.out.println(ycls);

    //returns false if the compilation failed 
    //or no compiler is available
    boolean retval = Compiler.compileClass(xcls);
    System.out.println("Return Value = " + retval); 
  }
}
class subMyClass extends MyClass {
  //sub class
} 

The output of the above code will be:

class MyClass
class subMyClass
Return Value = false

❮ Java.lang - Compiler