When Adobe ColdFusion (ACF) 2025 came out, one of the features that I was most excited about was the ability to create isolated class loaders with the createObject() method by explicitly supplying an array of JAR files. I'd been doing this in Lucee CFML for years; so, when ACF finally caught up, I was thrilled. In my excitement, however, I missed the fact that this also works with the new java() operator as well. Admittedly, I haven't used too much of the more intimate mechanics of ACF's Java integration; so my oversight isn't surprising. But, for anyone else who may have missed it, here's a quick demo.
In the following CFML, I'm going to load an instance of the jSoup library in two ways:
- The old-school
createObject()method. - The new-school
new java()method.
I don't have this JAR file loaded in my lib folder; so what we're looking for here is the ability to load the Java class without throwing a "class not found" exception.
<cfscript>
// We're going to create an isolated class loader(s) using this collection of JARs.
jarPaths = [
expandPath( "/core/vendor/jsoup-1.23.2/jsoup-1.23.2.jar" ),
];
// Create new instance of Java class using traditional createObject().
jSoup = createObject( "java", "org.jsoup.Jsoup", jarPaths );
// Create new instance of Java class using "new java" construction.
jSoup2 = new java( "org.jsoup.Jsoup", jarPaths );
writeDump([
"With createObject": jSoup.getClass().getName(),
"With new java": jSoup2.getClass().getName(),
]);
</cfscript>
As you can see, the new Java() method (?operator?) take the target class as the first argument and the list of JAR files as the second argument. Just like the createObject(); but without the need to specify the type of object. And, when we run this ColdFusion code, we get the following output:
- With createObject:
org.jsoup.Jsoup - With new java:
org.jsoup.Jsoup
Works like a charm!
And just to be clear, if I remove the jarPaths argument from either of the two invocations, I get the following error:
java.lang.ClassNotFoundException: org.jsoup.Jsoup
at coldfusion.bootstrap.BootstrapClassLoader.loadClass(BootstrapClassLoader.java:283)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
at coldfusion.runtime.java.JavaProxyFactory.getProxy(JavaProxyFactory.java:126)
at coldfusion.runtime.ProxyFactory.getProxy(ProxyFactory.java:84)
at coldfusion.runtime.CFPage._createObjectProxy(CFPage.java:10308)
at coldfusion.runtime.CFPage._CreateObject(CFPage.java:10249)
at coldfusion.runtime.CFPage._CreateObject(CFPage.java:10168)
at coldfusion.runtime.CFPage._CreateObject(CFPage.java:10090)
at coldfusion.runtime.CFPage.CreateObject(CFPage.java:9971)
The tighter Java integration is a real gap in my understanding of recent ColdFusion releases. It's been on my learning roadmap for a while; I just haven't had the need for it yet. But I'll definitely use the new java operator starting today.
Reader Comments
Post A Comment — ❤️ I'd Love To Hear From You! ❤️
Post a Comment →