Skip to content

Dynamic Compilation

markmandel edited this page Feb 16, 2012 · 2 revisions

To ease the pain of compiling and archiving your Java classes into .jar files to be used by JavaLoader, JavaLoader now provides the ability to dynamically compile and load your Java source code.

Java 1.6+ is required for dynamic compilation, as it leverages the Java 1.6 Compiler API.

Requirements

  • ColdFusion 8 or 9
  • Java 1.6+
  • API Documentation

ColdDoc Documentation

Reference

To use JavaLoader to load Classes that are stored in a file path, and dynamically compile your Java source, you can use it like so:

createObject("component", "javaloader.JavaLoader").init([loadPaths] [,loadColdFusionClassPath] [,parentClassLoader] [,sourceDirectories] [,compileDirectory] [,trustedSource]); 

There are six arguments for classloading that are possible to use to configure how and what JavaLoader loads and compiles.

Parameter: loadPaths

If you are compiling Java code that have dependencies on other libraries, include them with this argument. For more details on loadPaths, see Class Loading

Parameter: loadColdFusionClassPath

Defaults to false.
This parameter has no effect on dynamic compilation, but may be useful for Class Loading.

Parameter: parentClassLoader

Defaults to null
This parameter has no effect on dynamic compilation, but may be useful for Class Loading.

Parameter: sourceDirectories

Defaults to arrayNew(1)
This is an array of directories that you wish to be compiled and loaded by JavaLoader.
It should be noted that JavaLoader will load all the files in the provided source directories, which is very useful if your Java code needs to find resource files on the classpath.

Parameter: compileDirectory

Defaults to ./tmp folder with in /javaloader
This is the folder that JavaLoader creates the .jar file from the source code dynamically. You will only need to change this if there is an issue writing to that directory on your system.

Parameter: trustedSource

Defaults to false.
By default, JavaLoader will check to see if any of your source has changed, and recompile the source code before returning you an object on create(). On production systems, it is useful to turn this off for performance reasons by setting trustedSource to 'true'.

It is also worth noting that when trustedSource is 'true', JavaLoader will retain the compiled JAR file, and reuse it on Server restarts, mitigating the server startup performance cost of recompilation.

If you have set up your dynamic compilcation correctly, then you can simply instantiate the Java objects that have been compiled (or loaded) in the same way as you would in Class Loading.