I have been experimenting with Java libraries in CF8 and Railo to see how easy it is to load up a custom Java class in each. I know that we can simply add the .jar into the lib directory in ColdFusion and Railo and it will automatically be ready to use after a restart.
<cfset test = CreateObject( "java", "my.custom.TestObject" ) />
However, this means that part of your application code is floating around somewhere it really shouldn’t. So how easy is it to keep the .jar local to the application directory and use it?
ColdFusion and ColdFusion Class Path
Well in ColdFusion under Java and JVM there is a section Coldfusion Class Path, and it is as simple as adding the directory to your .jar files in that text area. So lets test it out, we create a Java project in our IDE and create a package my.custom and add the class TestObject:
package my.custom; public class TestObject { private String something; public TestObject() { this.something = ""; }; public void setSomething( String something ) { this.something = something; }; public String getSomething() { return this.something; }; };
Then we export this as a .jar file testobject.jar and add it to our application lib directory. Then we go into our ColdFusion administrator and add the reference to the lib directory c:\webs\websites\app\lib in the ColdFusion Class Path text area and restart coldfusion. In our coldfusion template we call:
<cfset testInstance = CreateObject( "java", "my.custom.TestObject" ).init() /> <cfdump var="#testInstance#" /> <cfset testInstance.setSomething( "blah" ) /> <cfdump var="#testInstance.getSomething()#" />
And behold! The first dump outputs the representation of our custom Java class and the second outputs “blah”, brilliant!
Railo and CreateObject third argument
How about Railo then? Does that have the same solution? Well that I’m not sure about and am hoping that someone from the Railo group comes back to me with an answer on it although there is another method. We follow the steps above up to the point where the jar is saved into our lib file, then we do the following (I believe this is undocumented so I wouldn’t guarantee it will alway work like this):
<cfset testInstance = CreateObject( "java", "my.custom.TestObject", "/app/lib/testobject.jar" ).init() /> <cfdump var="#testInstance#" /> <cfset testInstance.setSomething( "blah" ) /> <cfdump var="#testInstance.getSomething()#" />
Et voila! the same in Railo. My only problem with this method is that you have to refer to the location of the .jar file wherever you want to create a custom java object in Railo. I really prefer the ColdFusion method from a maintainability standpoint, hopefully there is something analogous in Railo. Next .NET assemblies in ColdFusion and Railo…
Thanks! That was very helpfull!!
Reblogged this on Power to Build and commented:
We use Coldfusion at work. Fulltime CF developers have Dreamweaver and CF Server. I occasionally do some CF code to test. For this, I use Railo as my web server. Here is a post about using custom jars in Railo.