Tuesday, August 25, 2009

Connecting Coldfusion to a Java class.

For a while now I've want to know how to call Java from ColdFusion but didn't know how. After waking up any Java memories I had from making my "hello world" I found out it was really easy to do.

1. user.java
Remember Java is case sensitive unlike ColdFusion.

public class user{
public String getName(){

String name = "joe";
return name;
}
}

2. Drop the user.java file C:\JRun4\servers\cfusion\cfusion-ear\cfusion-war\WEB-INF\classes directory.
--NOTE I am using localhost mulitserver and this is where I put it.

3. Test.cfm
I create a test.cfm and create an object of user. I can now work with the object.

<cfset user = createObject("java", "user")>
<cfdump var="#user#">

<cfset name = user.getName()/>
<cfdump var="#name#">

No comments:

Post a Comment