JDK-4873959 : Include ability to compile source code written compiled and linked at runtime
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.4.2
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-06-04
  • Updated: 2003-06-04
  • Resolved: 2003-06-04
Related Reports
Duplicate :  
Description

Name: rmT116609			Date: 06/04/2003


A DESCRIPTION OF THE REQUEST :
Since javac is itself written in java it is possible to call the javac Main class to compile a source code file dynamically at runtime. Having done this one could use reflection to instantiate and fully access it (I saw an example of this on the web recently).

Unfortunately javac is in the sun.* package and you specifically recommend NOT calling code in sun.* package, and it isn't supported or true Java.

Therefore I would like to suggest including the ability to call javac in code at runtime (could either (i) move javac to a java.* or javax.* package, (ii) provide a wrapper of some sort, (iii) add a new class which compiles java source).

There may be security implications, but perhaps the permission system can be set up to require a specific permission to allow this.

JUSTIFICATION :
The advantage is that it would enable macro programming and dynamic plugin support to be provided in Java applications thus making them much more dynamic.

Reflection allows some degree of flexibility, but requires the code to be compiled at design time so it is only partially dynamic.

Currently you couldn't provide a VBA type architecture within an application that allows writing code and executing it at runtime.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
More dynamic Java applications which allow code to be compiled, linked and executed all at runtime.

---------- BEGIN SOURCE ----------
Something like:

public class JavaCompiler extends Object
{
    // constructors
    ...

    // methods
    public boolean compile (String source_code)
    {
        // compile the source and return either a true/false
        // result or the class file for the compiled class
    }

    // etc as appropriate
}

Then in code:

(1) get the source to be compiled as a String
(2) call the compile method. Output could be a boolean result (a la javac) or the Class object for the compiled class.
(3) get the Class object and create an instance
(4) call that instances methods.

Or you could use reflection after step (2).
---------- END SOURCE ----------
(Review ID: 187087) 
======================================================================

Comments
EVALUATION See JSR 199. ###@###.### 2003-06-04
04-06-2003