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);
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson