Using CFObject For Java Integration

Question: I need to find out what exactly is required in order to utilize Java class files with the CFOBJECT tag. I really need to know where they need to be stored and what the different values should be within the Java section of the ColdFusion Administrator.

Charlie's answer:

First let me point out I have a page full of other resources on marrying cf and java. That info was written for the CF4.5 and 5 releases, as is this answer, but much of it still applies in CFMX.

Second, Guy Rish did a real good job with an article series in the CFDJ (www.coldfusionjournal.com) on incorporating Java and CF. Links to the articles are offered on that "marrying cf and java" page above.

As the questioner asks, the admin setup is indeed one of the keys. We're referring to the "JVM and Java Settings" in the Administrator.

There are two big kickers in getting this working (assuming, of course, one understands Java, and compiling classes, and classpaths, etc.)

Installing Java The first key to being able to run java class files in CF is to install a Java Virtual Machine (JVM) on the ColdFusion server box. The good news is that this is done automatically in CFMX. For those running CF 4.5 or 5, you need to install Java yourself. You can find one at http://java.sun.com/j2se/downloads.html. You will likely want to use the 1.3 or greater version. Setting the "Java Virtual Machine Path"

Next, those using 4.5 or 5 must set the admin field called "Java Virtual Machine path" after installing the JVM. (In CFMX, it's already filled in on installation of CFMX, set by default to [CFusionMX-home]/runtime/jre. You can skip to the next section.)

This "JVM Path" field prompt is a bit of a misnomer. It's indeed the location of the jvm, but in CF 4.5 and 5 it's the name of the file not just the location. So if your jvm.dll is located in D:\jdk1.3\jre\bin\classic\, then the value to put there is D:\jdk1.3\jre\bin\classic\jvm.dll (note the addition of jvm.dll). If you're running Unix or Linux, the filename is libjvm.so, according to wording on this screen in the CF 5 admininstrator.

If in Windows you don't know where your jvm.dll is, just use the Windows "file find" feature (or whatever os tool you have) to search your hard drive for jvm.dll. There may be a few. Any will do, typically, though you may want to check the version and be sure to use a recent JVM. Go to the DOS command line and change to the directory containing the jvm.dll. Use the java -version command to determine the version.

Setting the "Classpath" in the CF Administrator

Then the second key, if you then want to execute your own Java classes, is pointing the "class path" field in the CF administrator to wherever you have java class (or .jar) files. Let's say you store some code in c:\myjavaclasses. You'd want to add that path in the "class path" field. You can enter multiple directories, so if you're pointing to some classes belonging to another application, put that path there. Seperate multiple directories with a semi-colon in CF 4.5 and 5, and with commas in CFMX. If you change the class path field, you need to restart CF.

Setting the "CFX Jar Path" (in CF 4.5 or 5)

The last important field is the "cfx jar path" (though some of the other fields are important in certain implementations of java on certain os's). This field no longer exists in the CFMX admin. Even in CF 4.5 or 5, you don't really need to worry about the cfx jar path unless you're using cfx java custom tags.

CFX custom tags can integrate with CF by being able to read/write CF variables, queries, etc. See more on that in Chapter 18 of the "Developing Web Apps" book in CF 4.5, Chaper 21 in the "Developing ColdFusion Applications" book in CF5, or Chapter 12 in "Developing ColdFusionMX Applications with CFML" in CFMX.

But if one is doing cfx's, then in CF 4.5 or 5 the "cfx jar path" should point to the location of CF's built-in cfx.jar (which includes the libraries for doing this sort of thing). By default, it's in the cfusion\java\classes directory where CF is installed. Put the full drive and path in here.

Calling Your Java Classes

Now, calling a class with CFOBJECT is pretty simple. You just use:

<CFobject action="CREATE" type="JAVA" name="varname" class="classname">

where classname is the name of the class you want to run, and varname is the name of the variable which will hold the reference to the object. If you know the names of the object's methods and properties, you can refer to them, such as:

<CFset x = varname.method(attribute)>

where method is the name of some method in the object (java class) and attribute is something you may want to pass to that method.

Or if you wanted to refer to a property, you could do so as:

<CFoutput>#varname.property#</cfoutput>

If you wanted a demonstrable example, here is some simple java code that takes in a score and returns a grade:


public class grades {

public static char letterGrade(int numGrade) {

	// Returns the letter grade corresponding to
	// the numerical grade, numGrade.
	
	if (numGrade >= 91)
	   return 'A';   // 90 or above gets an A
	else if (numGrade >= 80)
	   return 'B';   // 80 to 89 gets a B
	else if (numGrade >= 65)
	   return 'C';   // 65 to 79 gets a C
	else if (numGrade >= 50)
	   return 'D';   // 50 to 64 gets a D
	else
	   return 'F';   // anything else gets an F
	
	}  // end of function letterGrade()
	 
}

If you put this in a directory pointed to on the the "classpath"you set above, and compile it successfully (I'll have to leave that step to your finding out more), you should be able to call it in CF:

<CFobject action="CREATE" type="JAVA" name="x" class="grades"> <CFset ret = x.letterGrade(90)> <CFoutput>#chr(ret)#</cfoutput>

Restarting the Server on Change of Classes

Once you've compiled a class and run it via CFOBJECT, you won't see any changes made in a recompilation of that object until the CF server is restarted.

However, CF 5 did add a "Dynamic Class Load Path" field to the administrator. If you specify a directory there, then CF 5 should detect a change to a class and use that on the next instantiation of an object in that class.

In CFMX, the field is no longer there, but you can instead tell CFMX to reload classes by changing the file [cfmx_home]\wwwroot\WEB-INF\jrun-web.xml, adding the following line within the existing <jrun-web-app> element: <reload>TRUE</reload> Restart the server. CFMX will now reload classes from [cfmx_home]\wwwroot\WEB-INF\classes as well as classes in jar/zip archives in [cfmx_home]\wwwroot\WEB-INF\lib. Note: The [cfmx_home]\wwwroot\WEB-INF\classes directory must contain class files only, not class archives. Java class archives belong under the [cfmx_home]\wwwroot\WEB-INF\lib directory

Of course, if one is calling pre-built classes or using home-grown ones in production where changes aren't frequent, the concern over being able to dynamically load changed classes is not important. Indeed, it would generally be unwise from a performance perspective to set up dynamic loading in production.

Hope that's all helpful.


| Home | ColdFusion | Articles | Presentations
| User Groups | Other Resources | Press Releases | Company

© 1998-2024, Charles Arehart, SysteManage
Our Practice Makes You Perfect