1

I noticed a compiler API in JDK 7. Does this mean that a Java program can recompile and alter the definition of a running program?

At the moment I know that products like JavaRebel can do this, but to offer a free download of a product excludes Javarebel which is a paid for product.

skaffman
  • 398,947
  • 96
  • 818
  • 769
yazz.com
  • 57,320
  • 66
  • 234
  • 385

5 Answers5

8

The Compiler API is already available in Java 6.

We can't change the code of already loaded classes - hot code replacement will still be limited to debugging, but we can compile source code from Java applications and load newly created classes at runtime.

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
3

Yes, it is possible. One way is to write a specialized class-loader and use a bytecode enhancement tool like ASM and alter parts of your application at runtime. It's also possible to achieve the same using a javaagent and again ASM or something similar.

Behrang
  • 46,888
  • 25
  • 118
  • 160
  • .. but this has nothing to do with the Compiler API. Correct me if I'm wrong. – Andreas Dolk Mar 15 '11 at 10:41
  • 1
    If the OP is only interested in using javac or the Compiler API to produce bytecode, you are right. But if his goal is to produce bytecode dynamically at runtime, then he can use ASM (or Javassist) with or without a javaagent as well. BTW, is the Compiler API bundled with the JVM download or is it only bundled with the JDK? – Behrang Mar 15 '11 at 10:53
  • That's what I found somewhere on the web: *The implementation of the Java Compiler is available in the tools.jar file (which is usually available in the \lib\tools.jar)*. So we need a JDK. – Andreas Dolk Mar 15 '11 at 12:28
3

As far as I know Java 7 introduces no additional methods of self-modification.

Some of the affects can already be done by using the compiler API and ClassLoaders, while some others can be done using the JVM TI.

But as far as I know Java 7 does not introduce any major changes to those APIs.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
3

There is a project to add JRebel-like class reloading functionality to OpenJDK 7, though it is not currently part of Open JDK 7: Dynamic Code Evolution VM (DCEVM)

It works through the normal JVMTI class redefinition mechanism.

reevesy
  • 3,452
  • 1
  • 26
  • 23
Ramon
  • 8,202
  • 4
  • 33
  • 41
  • 1
    It is not really correct that DCEVM is Rebel-like. No, those are two different approaches. JRebel is the agent that performs code transformations in order to enable class reloads, also it mapps the workspace and integrates with frameworks. DCEVM is instead a VM level approach for the sophisticated class redefinition (in debug mode). So the first one is pure Java, the latter one is an addition to VM - two quite different things – Anton Arhipov Mar 22 '11 at 11:31
  • 1
    Yes, the mechanisms are different but the goals are the same. DCEVM is a VM modification but it uses the standard JVMTI interfaces, just extending the kinds of class changes permitted. – Ramon Mar 23 '11 at 07:24
2

Javeleon offers functionality along these lines; it is available at no cost but not freely redistributable.

The Compiler API (JSR 199) has little to do with code reloading or self-modification, except insofar as a program which wishes to load new bytecode in some way might use 199 to build that bytecode from Java source, rather than using an assembler API like ASM.

Update:

It looks like Javelon is no longer available as it has been acquired by zeroturnaround (creators of JRebel)

From the Javelon homepage:

Note: Javeleon will no longer be available for download as a standalone product.

reevesy
  • 3,452
  • 1
  • 26
  • 23
Jesse Glick
  • 24,539
  • 10
  • 90
  • 112