Day 8: ColdFusion 10 and Enhanced Java Integration

ColdFusion built on J2EE compliant technology and this allow you take advantage of integrate with J2EE component and ColdFusion 10 enhanced this capabilities. In previous version we are able to create object of Java classes, using JSP tag etc…etc. but there was pain with using custom java class as you may need to store class file into web_root/WEB_INF/classes folder and you need to restart ColdFusion server every time you update your class file. 

I am using custom Java class in many places where ColdFusion limit or need performance improvement. In past (ColdFusion 8) we were building up PDF from bunch of photos and this was taking lots of time and memory. We just create custom java class that create PDF the way we want it and improve performance around 10 times.

There is javaLoader project which let you execute your custom class without moving it to classes folder and restarting ColdFusion service and now inbuilt with ColdFusion 10. ColdFusion 10 added capability to integrate your custom java code without moving class file to web_root/WEB_INF/classes folder and more importantly without restarting coldfusion service. Added application setting "javaSettings" where you can specify your java library files location which you want to load in coldfusion pages. You may need to add setting in Application.cfc file and syntax look like below.

[code:cf]<cfset THIS.javaSettings = {LoadPaths = [".\javalib\"], loadColdFusionClassPath = true, reloadOnChange = true,watchInterval=30}>[/code]

Settings basically structure with following elements.

  • LoadPaths : Array of path to directories, class or Jar files.
  • loadColdFusionClassPath : Indicates whether to load file from ColdFusion default lib folder and default is false.
  • reloadOnChange : Indicates whether to reload update jar or class files without restarting to coldfusion service and default is false
  • watchInterval : No of seconds after system will look for modified class or jar files.
  • watchExtensions : Which extension to be consider for reload changes. Default are .class and .jar.

For above setting system will look for javalib directory for all java classes or jar files while creating object. If you are adding/modifying javasettings in Application.cfc you may need to restart coldfusion service but not restart required if reloadOnChange is set to true and your jar/class file updated.

Let’s create sample java class Hello.java and store in javalib folder and compile it to create .class file.

[code:java]
public class Hello{
public String sayHello(){
return "Hello World!!!!";
}
public String sayHello(String name){
return "Hello " + name + "!!!!!";
}
}
[/code]

 Ok now its time to access it from CFM files.

[code:cf]
<cfobject type="java" class="Hello" name="myObj"> 
<cfoutput > 
#myObj.sayHello()# <br/>
#myObj.sayHello(‘Pritesh’)# 
</cfoutput>
[/code]

And output is…
Hello World!!!! 
Hello Pritesh!!!!!

Now change in java code and recompile it and refresh your CFM page. Awesome changes reflected without restarting my service (you may need to wait for changes to reflect if watchInterval setup in Application.cfc file).

Hope this help.