Skip to content
Robert edited this page Apr 3, 2014 · 5 revisions

This is the core function of JavaLoader. To take a Java Library that is stored on your file system, and make it available for you to use in your ColdFusion application without having to load it into the ColdFusion class path.

Requirements

  • ColdFusion 7, 8 or 9
  • Java 1.4+

API Documentation

ColdDoc Documentation

NetworkClassLoader JavaDoc

Reference

To use JavaLoader to load Class that are stored in a file path, you can use it like so:

createObject("component", "javaloader.JavaLoader").init(loadPaths [,loadColdFusionClassPath] [,parentClassLoader]); 

There are three arguments for classloading that are possible to use to configure how and what the JavaLoader loads.

Parameter: loadPaths

An array of directories of classes, or paths to .jar files to load.

An example would be:

loadPaths = ArrayNew(1);
loadPaths[1] = expandPath("icu4j.jar");
loadPaths[2] = expandPath("log4j.jar");

Parameter: loadColdFusionClassPath

Defaults to: false
Loads the ColdFusion libraries with the loaded libraries.
This used to be on by default, however now you must implicitly set it to be true if you wish to access any of the libraries that ColdFusion loads at application start up.

Parameter: parentClassLoader

Defaults to: null (Expert use only) The parent java.lang.ClassLoader to set when creating the URLClassLoader.
Note - when setting loadColdFusionClassPath to 'true', this value is overwritten with the ColdFusion classloader.

To create an instance of a Java Class, you then only need to call:

javaloader.create(className).init(arg1, arg2...); 

Parameter: className

The name of the Java Class to create.

This works exactly the same as createObject("java", className), such that simply calling create(className) gives you access to the static properties of the class, but to get an instance through calling the Constructor you are required to call create(className).init();

For example:

javaloader.create("org.apache.log4j.Logger").init("my log"); 

###class instantiation### After loading a class via

proxyToClass = javaloader.create(String classname)

a proxy and not an instance of this class is returned. The proxy can be used to access static properties of this class, but to retreive an instance you have to invoke the classes constructor by calling

classInstance = proxyToClass.init([constructorarguments])

Note: In certain cases the init method does only return one ClassInstance per ClassProxy! If you want to make sure you have a clean and fresh Java-Instance after a call to init under every circumstances strictly keep to the envisioned instantiation way of e.g.:

classInstance1 = javaloader.create("myClassName").init([arguments]);
classInstance2 = javaloader.create("myClassName").init([arguments]);
classInstance3 = javaloader.create("myClassName").init([arguments]);
...

don't reuse the proxy! Detailed Reason