CFLib.org – Common Function Library Project

createJavaObject(directory)

Last updated October 19, 2004

author

Wayne Graham

Version: 1 | Requires: CF6 | Library: UtilityLib

Description:
Instantiates Java objects in virtual paths so you do not need to change the classpath for your application.

Return Values:
Returns a Java object.

Example:

<cfscript>
    loader = createJavaObject("./");
    helloWorld = loader.loadClass("GpgTool").newInstance();
</cfscript>

<cfdump var="#helloWorld#">

Parameters:

Name Description Required
directory Directory where the class can be found. Yes

Full UDF Source:

/**
 * Allows one to use virtual paths to Java objects.
 * Returns a Java object for a given virtual path. 
 * This allows you to place your Java class files 
 * anywhere you want. Based on Stephen Milligan's blog entry at http://www.spike.org.uk/blog/index.cfm?data=20040603#EBB52433-D565-E33F-353D2664926C59B5
 * 
 * @param directory      Directory where the class can be found. (Required)
 * @return Returns a Java object. 
 * @author Wayne Graham (wsgrah@wm.edu) 
 * @version 1, October 19, 2004 
 */
function createJavaObject(directory){
    var URLObject = createObject("java", "java.net.URL");
    var ArrayClass = createObject("java","java.lang.reflect.Array");
    var loader = createObject("java","java.net.URLClassLoader");
    var URLArray = "";
    
    directory = replace(expandPath(directory), "\", "/", "all");        
    URLObject.init("file:" & directory);
    URLArray = createObject("java", "java.lang.reflect.Array").newInstance(URLObject.getClass(), 1);
        
    ArrayClass.set(URLArray,0,URLObject);
    return loader.init(URLArray);
}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson