Class Compiler In Java
The class compiler provides support to java to native code compilers and related services. When JVM starts, it determines if a system property in java exists.
If so, it is assumed to be the name of the library(Whose exact location and type is platform dependent).
The Compiler class encapsulates a facility for compiling Java classes to native code. As provided by Sun, the methods of this class do not actually do anything. However, if the system property java.compiler has been defined and if the method System.loadLibrary() is able to load the library named by the property, the methods of this class use the implementations provided in the library. The Compiler class has no public constructors, so it cannot be instantiated.
The loadLibrary() method in class-System is called to load the library. If the library loading is successful, the function name java.lang.Compiler.start() on the library is called. In case there is no compiler, these methods do nothing.
Normally, the javac command compiles only the file that you specify on the command line, but you can coax javac into compiling more than one file at a time by using any of the following techniques:
If the Java file you specify on the command line contains a reference to another Java class that’s defined by a java file in the same folder, the Java compiler automatically compiles that class, too.
You can list more than one filename in the javac command. The following command compiles three files
- If you need to compile a lot of files at the same time but don’t want to use a wildcard
- (perhaps you want to compile a large number of files but not all the files in a folder),
- you can create an argument file, which lists the files to compile.
- In the argument file, you can type as many filenames as you want, using spaces or line breaks to separate them. Here’s an argument file named TestPrograms that lists three files to compile:
- You can compile all the programs in this file by using an @ character, followed by the name of the argument file on the javac command line.
The structure of class Compiler is given as:
public final class java.lang.Compiler extends java.lang.Object{ //methods: public static native Object command(Object obj); public static native boolean compileClass(Class aclass); public static native boolean compileClasses(String classes); public static native void disable(); public static native void enable(); }
The details of the methods are as follows:
public static native Object command(Object obj);
public static native Object command(Object obj)method directs the compiler to perform an operation specified by the given argument. The available operations, if any, are determined by the compiler library.
This method returns a value determined by the compiler library, or null if no compiler library is loaded.
Parameter
obj -The permissible value and its meaning are determined by the compiler library.public static native boolean compileClass(Class aclass);
public static native boolean compileClass(Class aclass) method requests the compiler to compile the specified class to native code.
This method returns true if the compilation succeeds, or false if the compilation fails or no compiler library is loaded.
Parameter
aclass-The class to be compiled to native code.public static native boolean compileClasses(String classes);
public static native boolean compileClasses(String classes) method requests the compiler to compile all of the classes named in the string.
This method returns true if the compilation succeeds or false if the compilation fails or no compiler library is
loaded.
Parameter
classes-A string that specifies the names of the classes to be compiled.public static native void disable();
public static native void disable() method disables the compiler if one is loaded.
public static native void enable();
public static native void enable() method enables the compiler if one is loaded.
Apart from these Compiler class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)