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.

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

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.

	public class Hello{
	public String sayHello(){
	return "Hello World!!!!";
	}
	public String sayHello(String name){
	return "Hello " + name + "!!!!!";
	}
	}
	

 Ok now its time to access it from CFM files.

	<cfobject type="java" class="Hello" name="myObj"> 
	<cfoutput > 
	#myObj.sayHello()# <br/>
	#myObj.sayHello('Pritesh')# 
	</cfoutput>
	

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.

Comments

Kirill

Hi. Look I have a problem with loading java classes into CF10. I do everything as your example says, but nothing is outputted! Using your example "Hello World!!!!" and "Hello Pritesh!!!!!" are simply not outputted into the page. More than that: everything on the page that goes after cfobject tag is simply not outputted to the browser. It is as if cfobject works like cfabort!

I have a folder "wwwroot/java". In this folder I have:

Application.cfc

THIS.javaSettings = {LoadPaths = [".\javalib\"], loadColdFusionClassPath = true, reloadOnChange = true,watchInterval=30};

Index.cfm

<cfdump var="Hi!">

<cfobject type="java" class="Hello" name="myObj">

<cfoutput >

#myObj.sayHello()# <br/>

#myObj.sayHello('Pritesh')#

</cfoutput>

<cfdump var="Bye!">

And a folder wwwroot/java/javalib with two files:

Hello.java (from you example)

Hello.class (a file I compiled with the following command: "javac Hello.java")

So when I run my index.cfm, the only thing that I see in the browser is "Hi!" from the 1st cfdump. And that's it. There is nothing else after "Hi!". Just blank page. No errors no nothing.

I tried examples from Adobe site too, the same thing with them.

I think that may be there are problems with compiled class? My be I do it wrong? I tried googling this but nothing comes up.

Please if you have any insights on how to make this work, or what could cause the problem, I would very much appreciate if you could advise. I am hoping you could find some time to reply (even if it is "no idea, dude!").

Thank you.

October 19, 2012, 11:30 PM
Reply
Kirill

Hi again! Regarding my previous post. I have found that if I use JavaCompiler that comes in the bundle with JavaLoader(https://github.com/markmandel/JavaLoader), then the problem disappears. But if I do compilation with javac, then the problem appears again. Obviously there are some differences in .class files build by JavaCompiler, and by javac. And I still need to be able to use .class (and .jar) files, that are built with external tools (not JavaCompiler). Do you think you might know reasons why the results of javac and JavaCompiler are different?

Thank you

October 20, 2012, 12:57 AM
Reply
NOTE : Comments are moderated, and will not appear until the author has approved them.
Post a Comment
  1. Leave this field empty

Required Field