Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Class File
Next article icon

Compiler Class in Java

Last Updated : 09 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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 of Java Compiler Class 

1. command()

The 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: It returns the compiler-specific value.

Exception: It throws NullPointerException.

2. compileClass() 

The java.lang.Compiler.compileClass() compiles the specified class. 

Syntax: 

public static boolean compileClass(Class c)

Parameters: 

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

Syntax: 

public static boolean compileClasses(String string)

Parameters: 

  • str: name of the class to be compiled.

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

Exception: It throws NullPointerException.

Example:

Java

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

Output:

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.



Next Article
Java Class File

M

Mohit Gupta_OMG
Improve
Article Tags :
  • Java
  • Java-lang package
Practice Tags :
  • Java

Similar Reads

  • Java Class File
    A Java class file is a file containing Java bytecode and having .class extension that can be executed by JVM. A Java class file is created by a Java compiler from .java files as a result of successful compilation. As we know, a single Java programming language source file (or we can say .java file)
    5 min read
  • 10 Best Java Compilers in 2025
    Java Compilers refers to a program that takes the text file work of a software developer and compiles it into a platform-independent Java file. The Java compilers mainly include the Java Programming language compilers (javac), the Eclipse compiler for Java(ECJ), the GNU, and Jikes. The Java Compiler
    7 min read
  • Java.Lang.Double Class in Java
    Double class is a wrapper class for the primitive type double which contains several methods to effectively deal with a double value like converting it to a string representation, and vice-versa. An object of the Double class can hold a single double value. Double class is a wrapper class for the pr
    4 min read
  • Closures in Java with Examples
    A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every method must be part of some class that is diffe
    5 min read
  • Local Inner Class in Java
    Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the
    5 min read
  • How to Execute a .class File in Java?
    A Java Class file is a compiled java file. It is compiled by the Java compiler into bytecode to be executed by the Java Virtual Machine. Step #1: Compile the .java File Open Terminal (Mac) or Command Prompt (Windows). Navigate to the folder containing the java file and type the following command to
    1 min read
  • Coercion in Java
    Conversion of one data type to another is nowadays easy as many languages including Java supports the conversion as a convenience to the programmer. Here conversion can be done in two ways i.e. implicit or explicit conversion. implicit conversion: This type of conversion is automatically done by the
    3 min read
  • Byte Code in Java
    Byte Code Byte Code can be defined as an intermediate code generated by the compiler after the compilation of source code(JAVA Program). This intermediate code makes Java a platform-independent language. How is Byte Code generated? Compiler converts the source code or the Java program into the Byte
    2 min read
  • Object Class in Java
    Object class in Java is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class. If a class does not extend any other class then it is a direct child class of the Java Object class and if it extends another class then it is indirectly derived. The Ob
    8 min read
  • Types of Classes in Java
    A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien
    8 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences