[Tip of the Week] Using JavaLoader in WireBox

If you’re using external Jar files in your project, you may be loading them at run time with Mark Mandel’s JavaLoader project. (CF10 allows for dynamic loading of Jar files, but for people still on CF8, or 9, JavaLoader is the de facto method)
If you’re manually creating and using the JavaLoader CFC, please remember that WireBox has a dedicated “JavaLoader” namespace in its mapping DSL to handle this for you using ColdBox’s JavaLoader Plugin. Here all you have to do:

Put a javaloader_libpath setting in your config file that tells ColdBox where the your Jars are:

ColdBox.cfc
// custom settings
settings = {
javaloader_libpath = “/includes/jars/”
};

If you have more than one Jar directory, simply pass in a comma-delimited list, OR an array of paths. Your choice.

Now you can inject that class into your components by using the “javaloader” namespace and following it by the Java classpath:

component {
property name=“UASparser” inject=“javaLoader:cz.mallat.uasparser.UASparser”;
}

That will inject the Java Class definition (which is fine for static classes). If you need an instance, you’ll want to call the init() method to actually instantiate it.

function onDIComplete() {
oUASparser = UASparser.init(ExpandPath("/includes/uas.ini"));
}

That’s not the only way-- this one-liner accomplishes the same thing:

oUASparser = getModel(dsl=“javaloader:cz.mallat.uasparser.UASparser”).init(ExpandPath("/includes/uas.ini"));

More info here: http://wiki.coldbox.org/wiki/Plugins:JavaLoader.cfm
And here: http://wiki.coldbox.org/wiki/WireBox.cfm#ColdBox_Namespace

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com