on the fly : Java Glossary


on the fly
Introduction Learning More
Sample Code Links


Creating Java byte code while a program is running either by using javac.exe or the deprecated undocumented sun.com.javac.Main or JavaCompiler, or by composing the byte code directly.

You might use the technique for example to compose customised byte code for a particular spreadsheet or regex then let Hotspot turn it into native code, rather than using in interpretive spreadsheet or regex engine that Hotspot has turned into native code. The advantage is speed.

Sample On The Fly Composition, Compile and Execute

How we did it in the Olden Days

Beware! The following example uses an undocumented, deprecated technique calling com.sun.tools.javac.Main. Normally you would use javax.tools.JavaCompiler

Here is what the dynamically generated Hypotenuse class looks like:

One additional problem with this technique is you need com.sun.tools.javac.Main which lives in tools.jar which is not traditionally on the classpath.

How we do it Now


You may wonder why in that sample code I wrote the generated java source to disk only to have javac read it back in again and why javac wrote the class files to disk only to have  Class.forName read them back in again. This is because the javac interface I am using is unofficial and kludgy. JavaCompiler in  Java version 1.6 lets you bypass the disk i/o.

Learning More

Oracle’s Javadoc on javax.tools.JavaCompiler class : available:

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number