Home > Articles

Compiling and Scripting

This chapter is from the book

This chapter introduces two techniques for processing code. You can use the compiler API when you want to compile Java code inside your application. The scripting API lets you invoke code in a scripting language such as JavaScript or Groovy.

8.1. The Compiler API

There are quite a few tools that need to compile Java code. Obviously, development environments and programs that teach Java programming are among them, as well as testing and build automation tools. Another example is the processing of Jakarta Server Pages—web pages with embedded Java statements.

8.1.1. Invoking the Compiler

It is very easy to invoke the compiler. Here is a sample call:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
OutputStream outStream = . . .;
OutputStream errStream = . . .;
int result = compiler.run(null, outStream, errStream,
   "-sourcepath", "src", "Test.java");

A result value of 0 indicates successful compilation.

The compiler sends its output and error messages to the provided streams. You can set these arguments to null, in which case System.out and System.err are used. The first argument of the run method is an input stream. As the compiler takes no console input, you can always leave it as null. (The run method is inherited from the generic javax.tools.Tool interface, which allows for tools that read input.)

The remaining arguments of the run method are the arguments that you would pass to javac if you invoked it on the command line. These can be options or file names.

8.1.2. Launching a Compilation Task

You can have more control over the compilation process with a CompilationTask object. This can be useful if you want to supply source from string, capture class files in memory, or process the error and warning messages.

To obtain a CompilationTask, start with a compiler object as in the preceding section. Then call

JavaCompiler.CompilationTask task = compiler.getTask(
   errorWriter, // Uses System.err if null
   fileManager, // Uses the standard file manager if null
   diagnosticListener, // Uses System.err if null
   options, // null if no options
   classes, // For annotation processing; null if none
   sources);

The last three arguments are Iterable instances. For example, a sequence of options might be specified as

Iterable<String> options = List.of("-d", "bin");

The sources parameter is an Iterable of JavaFileObject instances. If you want to compile disk files, get a StandardJavaFileManager and call its getJavaFileObjects method:

StandardJavaFileManager stdFileManager
   = compiler.getStandardFileManager(null, null, null);
Iterable<JavaFileObject> sources 
   = stdFileManager.getJavaFileObjectsFromStrings(List.of("File1.java", "File2.java"));
JavaCompiler.CompilationTask task
   = compiler.getTask(null, null, null, options, null, sources);

The getTask method returns the task object but does not yet start the compilation process. The CompilationTask class extends Callable<Boolean>. You can submit it to an ExecutorService for concurrent execution, or you can just make a synchronous call:

Boolean result = task.call();

8.1.3. Capturing Diagnostics

To listen to error messages, install a DiagnosticListener. The listener receives a Diagnostic object whenever the compiler reports a warning or error message. The DiagnosticCollector class implements this interface. It simply collects all diagnostics so that you can iterate through them after the compilation is complete.

var collector = new DiagnosticCollector<JavaFileObject>();
compiler.getTask(null, fileManager, collector, null, null, sources).call();
for (Diagnostic<? extends JavaFileObject> d : collector.getDiagnostics()) 
{
   System.out.println(d);
}

A Diagnostic object contains information about the problem location (including file name, line number, and column number) as well as a human-readable description.

You can also install a DiagnosticListener to the standard file manager, in case you want to trap messages about missing files:

StandardJavaFileManager stdFileManager
   = compiler.getStandardFileManager(diagnosticsListener, null, null);

8.1.4. Reading Source Files from Memory

If you generate source code on the fly, you can have it compiled from memory, without having to save files to disk. Use this class to hold the code:

public class StringSource extends SimpleJavaFileObject
{
   private String code;
 
   StringSource(String name, String code)
   {
      super(URI.create("string:///" + name.replace(".", "/") + ".java"), Kind.SOURCE);
      this.code = code;
    }

   public CharSequence getCharContent(boolean ignoreEncodingErrors)
   {
      return code;
   }
}

Then generate the code for your classes and give the compiler a list of StringSource objects:

List<StringSource> sources = List.of(
   new StringSource(className1, class1CodeString),
   new StringSource(className2, class2CodeString),
   . . .);
task = compiler.getTask(null, fileManager, diagnosticsListener, null, null, sources);

8.1.5. Writing Byte Codes to Memory

If you compile classes on the fly, there is no need to save the class files to disk. You can save them to memory and load them right away.

First, here is a class for holding the bytes:

public class ByteArrayClass extends SimpleJavaFileObject
{
   private ByteArrayOutputStream out;

   ByteArrayClass(String name) 
   {
      super(URI.create("bytes:///" + name.replace(".", "/") + ".class"), Kind.CLASS);
   }
    
   public byte[] getCode() 
   {
      return out.toByteArray();
   }
    
   public OutputStream openOutputStream() throws IOException 
   {
      out = new ByteArrayOutputStream();
      return out;
   }        
}

Next, you need to configure the file manager to use these classes for output:

var classes = new ArrayList<ByteArrayClass>();
StandardJavaFileManager stdFileManager
   = compiler.getStandardFileManager(null, null, null);
JavaFileManager fileManager
   = new ForwardingJavaFileManager<>(stdFileManager)
      {
         public JavaFileObject getJavaFileForOutput(Location location,
               String className, Kind kind, FileObject sibling)
               throws IOException
         {
            if (kind == Kind.CLASS)
            {
               ByteArrayClass outfile = new ByteArrayClass(className);
               classes.add(outfile);
               return outfile;
            }
            else
               return super.getJavaFileForOutput(location, className, kind, sibling);
         }
      };

To load the classes, you need a class loader (see Chapter 9):

public class ByteArrayClassLoader extends ClassLoader
{
   private Iterable<ByteArrayClass> classes; 
    
   public ByteArrayClassLoader(Iterable<ByteArrayClass> classes)
   {
      this.classes = classes;
   }
    
   public Class<?> findClass(String name) throws ClassNotFoundException
   {
      for (ByteArrayClass cl : classes)
      {
         if (cl.getName().equals("/" + name.replace(".", "/") + ".class"))
         {
            byte[] bytes = cl.getCode();
            return defineClass(name, bytes, 0, bytes.length);
         }
      }
      throw new ClassNotFoundException(name);        
   }
}

After compilation has finished, call the Class.forName method with that class loader:

ByteArrayClassLoader loader = new ByteArrayClassLoader(classes);
Class<?> cl = Class.forName(className, true, loader);

8.1.6. An Example: Dynamic Java Code Generation

In the Jakarta Server Pages (JSP) technology for dynamic web pages, you can mix HTML with snippets of Java code, for example:

<p>The current date and time is <b><%= LocalDateTime.now() %></b>.</p>

The JSP engine dynamically compiles the Java code into a servlet. In our sample application, we use a simpler example and generate code for a very simple worksheet format. Authors include text, Java expressions (enclosed in backticks), and Java statements (enclosed in triple backticks) into a worksheet. The worksheet is compiled into a Java class that prints the text and the values of the expressions, and executes the Java statements. Here is a sample worksheet:

What is your name?
```
var in = new Scanner(System.in);
String name = in.nextLine();
```
How old are you?
```
int age = in.nextInt();
```
Hello, `name`! Next year, you will be `age + 1`.

This is a very minimal text version of a programming notebook (https://docs.jupyter.org/en/latest/#what-is-a-notebook). The sheet is translated into a class whose main method has print statements to emit the text and expression values. The statements are included verbatim. One can envision an enhancement of this program that displays images and tables, just like a real programming notebook.

The buildSource method in the program of Listing 8.1 builds up this code and places it into a StringSource object. That object is passed to the Java compiler.

As described in the preceding section, we use a ForwardingJavaFileManager that constructs a ByteArrayClass object for every compiled class. These objects capture the class file that is generated when the class is compiled. After compilation, we use the class loader from the preceding section to load the class and execute its main method.

This example demonstrates how you can use dynamic compilation with in-memory source and class files.

Listing 8.1 compiler/CompilerTest.java

  1 package compiler;
  2 
  3 import java.io.*;
  4 import java.nio.file.*;
  5 import java.util.*;
  6 import java.util.List;
  7 
  8 import javax.tools.*;
  9 import javax.tools.JavaFileObject.*;
 10 
 11 /**
 12  * @version 1.2 2023-08-16
 13  * @author Cay Horstmann
 14  */
 15 public class CompilerTest
 16 {
 17    public static void main(final String[] args) 
 18          throws IOException, ReflectiveOperationException
 19    {
 20       JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
 21 
 22       var classFileObjects = new ArrayList<ByteArrayClass>();
 23 
 24       var diagnosticsListener = new DiagnosticCollector<JavaFileObject>();
 25 
 26       JavaFileManager fileManager 
 27          = compiler.getStandardFileManager(diagnosticsListener, null, null);
 28       fileManager = new ForwardingJavaFileManager<>(fileManager)
 29          {
 30             public JavaFileObject getJavaFileForOutput(Location location, 
 31                   String className, Kind kind, FileObject sibling) throws IOException
 32             {
 33                if (kind == Kind.CLASS) 
 34                {
 35                   var fileObject = new ByteArrayClass(className);
 36                   classFileObjects.add(fileObject);
 37                   return fileObject;
 38                }
 39                else return super.getJavaFileForOutput(location, className, kind, sibling);
 40             }
 41          };
 42 
 43 
 44       String mainClassName = "compiler.Main";
 45       String worksheetName = args.length > 0 ? args[0] : "compiler/worksheet";
 46       
 47       StandardJavaFileManager stdFileManager 
 48          = compiler.getStandardFileManager(null, null, null);
 49       var sources = new ArrayList<JavaFileObject>();
 50       for (JavaFileObject file : stdFileManager.getJavaFileObjectsFromStrings(
 51             List.of(mainClassName.replace(".",  "/") + ".java")))
 52          sources.add(file);
 53       
 54       JavaFileObject source = buildSource(mainClassName, Path.of(worksheetName));
 55       JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, 
 56          diagnosticsListener, null, null, List.of(source));
 57       Boolean result = task.call();
 58 
 59       for (Diagnostic<? extends JavaFileObject> d : diagnosticsListener.getDiagnostics())
 60          System.out.println(d.getKind() + ": " + d.getMessage(null));
 61       fileManager.close();
 62       if (!result)
 63       {
 64          System.out.println("Compilation failed.");
 65          System.exit(1);
 66       }
 67 
 68       var loader = new ByteArrayClassLoader(classFileObjects);
 69       Class<?> mainClass = loader.loadClass(mainClassName);
 70       mainClass.getMethod("main", String[].class).invoke(null, new Object[] { args });
 71    }
 72 
 73    /**
 74     * Builds the source for the class that implements the worksheet actions.
 75     * @param className the name of the class to be built
 76     * @param worksheet the file containing the worksheet instructions
 77     * @return a file object containing the source in a string builder
 78     */
 79    static JavaFileObject buildSource(String className, Path worksheet) 
 80          throws IOException
 81    {
 82       var builder = new StringBuilder();
 83       int n = className.lastIndexOf(".");
 84       if (n >= 0)
 85       {
 86          String packageName = className.substring(0, n); 
 87          className = className.substring(n + 1);
 88          builder.append("package ").append(packageName).append(";\n\n");
 89       }
 90       builder.append("import java.util.Scanner;");      
 91       builder.append("public class ").append(className).append(" {\n");
 92       builder.append("public static void main(String[] args) {\n");
 93       boolean verbatim = false;
 94       for (String line : Files.readAllLines(worksheet))
 95       {
 96          if (line.equals("```")) verbatim = !verbatim;
 97          else if (verbatim) builder.append(line).append("\n");
 98          else
 99          {
100             String[] fragments = line.split("`");
101             for (int i = 0; i < fragments.length; i++)
102             {
103                builder.append("System.out.print(");
104                if (i % 2 == 0) 
105                   builder.append("\"")
106                      .append(fragments[i].replace("\\", "\\\\").replace("\"", "\\\""))
107                      .append("\"");
108                else
109                   builder.append(fragments[i]);
110                builder.append(");\n");
111             }
112             builder.append("System.out.println()\n;");
113          }
114       }
115       
116       builder.append("} }\n");
117       return new StringSource(className, builder.toString());
118    }
119 }

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020