CFLib.org – Common Function Library Project

createJavaObject(directory)

Last updated October 19, 2004

Version: 1 | Requires: ColdFusion MX | Library: UtilityLib

 
Rated 2 time(s). Average Rating: 5.0

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:

view plain print about
<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:

view plain print about
<cfscript>
/**
 * 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);
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson