Blog

Michael Born

June 14, 2022

Spread the word


Share your thoughts

CFML makes it simple to instantiate and use native Java classes without leaving CFML-land. For example, we can use Java's SimpleDateFormat library to format a date object:

var dateFormatter = createObject( "java", "java.text.SimpleDateFormat" );
var formattedDate = dateFormatter.init( "yyyy-MM-dd'T'HH:mm: ssXXX" ).format( now() );

In order to use a third-party java library, however, such as the Jsoup HTML parser, you will need to load the jar into the CFML engine first.

There are two main ways to load a Java jar into CFML app:

  1. Using native CFML
  2. Using Javaloader

Loading Jars with native CFML

Add this to your web root's Application.cfc:

// Java Integration
this.javaSettings = {
   loadPaths               : [ expandPath( "./lib" ) ],
   loadColdFusionClassPath : true,
   reloadOnChange          : false
};

Then add the .jar file to the lib/ directory.

Once this is done, you can use the jar like this:

function getWikipediaPage(){
   var Jsoup = createObject( "java", "org.jsoup.Jsoup" );
   return Jsoup
      .connect( "https://en.wikipedia.org/wiki/The_Lord_of_the_Rings_(film_series)" )
      .get()
      .body()
      .text();
}

Loading Jars with JavaLoader

Javaloader is a CFML tool for loading java .jar files into your CFML application. Javaloader offers an improved developer experience, as there is no need to adjust application settings or restart the server after installation. With Javaloader, you can ship your dependencies with a module or library, and it "just works" without requiring further configuration.

box install cbjavaloader

Once javaloader installs, you can inject the Jsoup library into your components using the custom Javaloader syntax for Wirebox:

component{
    property name="Jsoup" inject="javaloader:org.jsoup.Jsoup";
    // ...
}

Once we’ve injected Jsoup, we can use Jsoup to fetch and parse the Wikipedia page for the LOTR film series:

function getWikipediaPage(){
   return Jsoup
      .connect( "https://en.wikipedia.org/wiki/The_Lord_of_the_Rings_(film_series)" )
      .get()
      .body()
      .text();
}

And that's how you use the Java "Jsoup" library in CFML!

Add Your Comment

(5)

Jun 15, 2022 00:42:47 UTC

by Charlie Arehart

Thanks, Michael. Always helpful for folks to be reminded of such things. That said, you could/should mention how lucee offers yet another option, in that you can name the library/jar as an arg to the createobject/cfobject. I do hope that someday Adobe will adopt that. Finally, somehow in the formatting of the code examples, the first one for application.cfc has the this.javasetting mashed into the opening comment: // Java Integrationthis.javaSettings (If anyone corrects that, feel free to delete this part of my comment, starting with "finally".)

Jun 15, 2022 00:49:45 UTC

by Anthony Ehrhardt

Which is better? And why would one use javaLoader over native CFML?

Jun 15, 2022 01:44:09 UTC

by Anthony Ehrhardt

Which is better? And why would one use javaLoader over native CFML?

Jun 16, 2022 14:21:33 UTC

by Michael Born

@anthony > Which is better? And why would one use javaLoader over native CFML? Read the paragraph under "Loading Jars with JavaLoader": > ...Javaloader offers an improved developer experience, as there is no need to adjust application settings or restart the server after installation. With Javaloader, you can ship your dependencies with a module or library, and it "just works" without requiring further configuration.

Jun 16, 2022 14:29:44 UTC

by Michael Born

@charlie - Good catch, thank you! I fixed the formatting on that code snippet. Yes, the post should probably mention Lucee's createObject() jar parameter. I avoided that to keep it simple, and because Ortus tends to focus on writing cross-engine compatible code. But agencies and companies building on Lucee only would certainly benefit from such a feature.

Recent Entries

BoxLang 1.0.0 Beta 7 Launched

BoxLang 1.0.0 Beta 7 Launched

We are pleased to announce the release of BoxLang 1.0.0-Beta 7! This latest beta version includes improvements and essential bug fixes, but more importantly it certifies the execution of ColdBox HMVC and TestBox.

What is BoxLang?

BoxLang is a modern dynamic JVM language that can be deployed on multiple runtimes: operating system (Windows/Mac/*nix/Embedded), web server, lambda, iOS, android, web assembly, and more. BoxLang combines many features from different progr

Luis Majano
Luis Majano
July 26, 2024
New BoxLang Feature: Java Method References and Higher-Order Functions

New BoxLang Feature: Java Method References and Higher-Order Functions

We’ve added more goodies to our BoxLang Java interop: method references and higher-order functions. CFML has never let you do these things, making Java Interop feel like a second-class citizen. But with BoxLang, we’re elevating Java integration to a new level.

Maria Jose Herrera
Maria Jose Herrera
July 26, 2024
Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Level Up Your ColdFusion Skills with our Virtual Live Training: ColdBox from Zero to Hero

Are you a CFML developer looking to take your skills to the next level? Look no further than the ColdBox from Zero to Hero Virtual Live Training! This intensive two-day course will equip you with the knowledge and expertise to build robust and scalable applications using ColdBox 7, the latest version of the most popular CFML MVC framework.

What You'll Learn:

  • Master the Fun...

Cristobal Escobar
Cristobal Escobar
July 24, 2024