CFLib.org – Common Function Library Project

createJavaObject(directory)

Last updated October 19, 2004
Download UDF

author

Wayne Graham                                      Wayne Graham

Version: 1 | Requires: ColdFusion MX | 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:

<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 (&#119;&#115;&#103;&#114;&#97;&#104;&#64;&#119;&#109;&#46;&#101;&#100;&#117;) 
 * @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>

Search CFLib.org


Latest Additions

Raymond Compton Raymond Compton added
structBlend
19 day(s) ago

Duncan Duncan added
IsZIPUK
19 day(s) ago

Todd Sharp Todd Sharp added
getTagContentAll
26 day(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Gerald Guido Gerald Guido added
ListReturnDuplicat...
1 month(s) ago

Created by Raymond Camden / Design by Justin Johnson