Java.lang Package Classes

Java Compiler - compileClasses() Method



The java.lang.Compiler.compileClasses() used to compile all classes whose name matches the specified string.

Syntax

public static boolean compileClasses(String string)

Parameters

string Specify the name of the classes to compile.

Return Value

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

Exception

Throws NullPointerException, If string is null.

Example:

The example below shows how to use java.lang.Compiler.compileClasses() 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.compileClasses("MyClass");
    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