User Guide Cancel

cfobject: Java or EJB object

 

Description

Creates and manipulates a Java and Enterprise Java Bean (EJB) object.

Syntax

<cfobject
class = "Java class"
type = "Java"
name = "instance name"
action = "create"
loadPaths= "path to jar"
>
<cfobject class = "Java class" type = "Java" name = "instance name" action = "create" loadPaths= "path to jar" >
<cfobject 
class = "Java class" 
type = "Java" 
name = "instance name" 
action = "create"
loadPaths= "path to jar"
>
Note:

You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

See also

cfcollectioncfexecutecfindexIsInstanceOfcfreportcfsearchcfwddxUsing Java objects in the Developing ColdFusion Applications

History

  • ColdFusion (2025 release): Added a new parameter, loadPaths. See example for more imformation.

Attributes

Attribute

Req/Opt

Default

Description

action

Optional

create

Only the default create action, which creates the object, is supported.

class

Required

 

The Java class.

name

Required

 

String; name for the instantiated component.

type

Required for Java

 

Object type. Must be java for Java and EJB objects.

loadPaths Required for Java   A path to JAR or an array of file paths that contain Java classes or JAR files.

Usage

To call Java CFXs or Java objects, ColdFusion uses a Java Virtual Machine (JVM) that is embedded in the process. You can configure JVM loading, location, and settings in the ColdFusion Administrator.Any Java class available in the class path that is specified in the ColdFusion Administrator can be loaded and used from ColdFusion, by using the cfobject tag.

Access Java methods and fields

  1. Call the cfobject tag, to load the class. See the example code.
  2. Use the initmethod with appropriate arguments, to call a constructor. For example:

<cfset ret = myObj.init(arg1, arg2)>
<cfset ret = myObj.init(arg1, arg2)>
<cfset ret = myObj.init(arg1, arg2)>

Calling a public method on the object without first calling the init method results in an implicit call to the default constructor. Arguments and return values can be any Java type (simple, array, object). ColdFusion makes the conversions if strings are passed as arguments, but not if they are received as return values.Overloaded methods are supported if the number of arguments is different.

Calling EJBs

To create and call EJB objects, use the cfobject tag. In the second example in the following section, the WebLogic JNDI is used to register and find EJBHome instances.

Example

<!--- Example of a Java Object, this cfobject call loads the class MyClass
but does not create an instance object. Static methods and fields
are accessible after a call to cfobject. --->
<cfobject
action = "create"
type = "java"
class = "myclass"
name = "myobj">
<!---- Example of an EJB - The cfobject tag creates the Weblogic Environment
object, which is used to get InitialContext. The context object is
used to look up the EJBHome interface. The call to Create() results
in getting an instance of stateless session EJB. --->
<cfobject
action = "create"
type = "java"
class = "weblogic/jndi/Environment"
name = "wlEnv">
<cfset ctx = wlEnv.getInitialContext()>
<cfset ejbHome = ctx.lookup("statelessSession.TraderHome")>
<cfset trader = ejbHome.Create()>
<cfset value = trader.shareValue(20, 55.45)>
<cfoutput>
Share value = #value#
</cfoutput>
<cfset value = trader.remove()>
<!--- Example of a Java Object, this cfobject call loads the class MyClass but does not create an instance object. Static methods and fields are accessible after a call to cfobject. ---> <cfobject action = "create" type = "java" class = "myclass" name = "myobj"> <!---- Example of an EJB - The cfobject tag creates the Weblogic Environment object, which is used to get InitialContext. The context object is used to look up the EJBHome interface. The call to Create() results in getting an instance of stateless session EJB. ---> <cfobject action = "create" type = "java" class = "weblogic/jndi/Environment" name = "wlEnv"> <cfset ctx = wlEnv.getInitialContext()> <cfset ejbHome = ctx.lookup("statelessSession.TraderHome")> <cfset trader = ejbHome.Create()> <cfset value = trader.shareValue(20, 55.45)> <cfoutput> Share value = #value# </cfoutput> <cfset value = trader.remove()>
<!--- Example of a Java Object, this cfobject call loads the class MyClass 
but does not create an instance object. Static methods and fields 
are accessible after a call to cfobject. ---> 
<cfobject 
action = "create" 
type = "java" 
class = "myclass" 
name = "myobj"> 


<!---- Example of an EJB - The cfobject tag creates the Weblogic Environment 
object, which is used to get InitialContext. The context object is 
used to look up the EJBHome interface. The call to Create() results 
in getting an instance of stateless session EJB. ---> 

<cfobject 
action = "create" 
type = "java" 
class = "weblogic/jndi/Environment" 
name = "wlEnv"> 

<cfset ctx = wlEnv.getInitialContext()> 
<cfset ejbHome = ctx.lookup("statelessSession.TraderHome")> 
<cfset trader = ejbHome.Create()> 
<cfset value = trader.shareValue(20, 55.45)> 
<cfoutput> 
Share value = #value# 
</cfoutput> 
<cfset value = trader.remove()>

Example 1

Book.java

public class Book {
private String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
public class Book { private String title; public Book(String title) { this.title = title; } public String getTitle() { return title; } }
public class Book {
    private String title;

    public Book(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

File.cfm

<cfscript>
path="#expandPath(".")#\MyApp.jar"
</cfscript>
<cfobject type="java" class="Book" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfset initObj = obj.init("ColdFusion Programming!!")>
<cfoutput>#initObj.getTitle()#</cfoutput>
<cfscript> path="#expandPath(".")#\MyApp.jar" </cfscript> <cfobject type="java" class="Book" name="obj" loadPaths="#["#ExpandPath(".")#"]#"> <cfset initObj = obj.init("ColdFusion Programming!!")> <cfoutput>#initObj.getTitle()#</cfoutput>
<cfscript>
    path="#expandPath(".")#\MyApp.jar"
</cfscript>

<cfobject type="java" class="Book" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfset initObj = obj.init("ColdFusion Programming!!")>
<cfoutput>#initObj.getTitle()#</cfoutput>

Example 2- loadPaths with cfobject and predefined class with fully qualified name

<cfobject type="java" class="com.myapp.Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfoutput>#obj.getMessage()#</cfoutput>
<cfobject type="java" class="com.myapp.Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#"> <cfoutput>#obj.getMessage()#</cfoutput>
<cfobject type="java" class="com.myapp.Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfoutput>#obj.getMessage()#</cfoutput>

Example 3- loadPaths with cfobject and predefined class without fully qualified name

<cfobject type="java" class="Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfoutput>#obj.getMessage()#</cfoutput>
<cfcatch>
<cfset errorMsg = "Error message: #cfcatch.message#">
<cfoutput>#cfcatch.message#</cfoutput>
</cfcatch>
</cftry>
<cfset error_msg = "The getMessage method was not found.">
<cfobject type="java" class="Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#"> <cfoutput>#obj.getMessage()#</cfoutput> <cfcatch> <cfset errorMsg = "Error message: #cfcatch.message#"> <cfoutput>#cfcatch.message#</cfoutput> </cfcatch> </cftry> <cfset error_msg = "The getMessage method was not found.">
<cfobject type="java" class="Test" name="obj" loadPaths="#["#ExpandPath(".")#"]#">
<cfoutput>#obj.getMessage()#</cfoutput>

<cfcatch>
    <cfset errorMsg = "Error message: #cfcatch.message#">
    <cfoutput>#cfcatch.message#</cfoutput>
</cfcatch>
</cftry>
<cfset error_msg = "The getMessage method was not found.">

Get help faster and easier

New user?