View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfobject: Java or EJB object PreviousNext

cfobject: Java or EJB object

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

<cfobject 
type = "Java"
action = "Create"
class = "Java class"
name = "object name">

cfcollection, cfexecute, cfindex, cfreport, cfsearch, cfwddx

Attribute

Req/Opt

Default

Description

type

Optional

 

Object type:

  • com
  • corba
  • java

(The other object types do not take the type attribute.)

action

Required

 

Create: Creates a Java or WebLogic Environment object.

class

Required

 

Java class.

name

Required

 

String; name for the instantiated component.

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, using the cfobject tag.

To access Java methods and fields, do the following steps:

  1. Call the cfobject tag, to load the class. See the example code.
  2. Use the init method with appropriate arguments, to call a constructor. For example:
    <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 below, the WebLogic JNDI is used to register and find EJBHome instances.

<!--- Example of a Java Object his 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()>

Contents > CFML Reference > ColdFusion Tags > cfobject: Java or EJB object PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


No screen name said on Apr 6, 2004 at 8:46 AM :
How do you register java classes in the administrator as suggested. It says you must put classes in the classpath within the cf admin module, but there is no such section for doing so?

Mike.
Calphool said on May 27, 2004 at 9:43 PM :
If I'm instantiating a java object and then invoking a method, is there a way to redirect stdout and stderr for the invokation so that I can capture it to a variable?
itsmeprash said on Oct 6, 2004 at 11:40 AM :
I wish there was an example on how to specify the package of the class. The syntax for cfobject is
<cfobject action = "create" type = "java" class = "myclass"
name = "myobj">

How do i specify the package and the classname?
No screen name said on Oct 20, 2004 at 4:25 PM :
Try using the fully qualified class name in the CLASS tag of the CFOBJECT tag, e.g,

<cfobject action="create" type="java" class="MYPACKAGE.MYCLASS" name="myobj">
Belikov said on Dec 16, 2004 at 7:29 AM :
Here is a pitfall that I found when coldfusion object is created inside the component method. I needed to call a particular constructor on a File class. The following code gave me an error:

<cfobject type="Java" action="create" name="File" class="java.io.File">
<cfset variables.lockFile = File.init(JavaCast("string","c:/tmp.1")) >

Using reflection, I found that variable File used this way refers to an object of class coldfusion.runtime.LocalScope. That object obviously does not have a method init with desired signature. To avoid this issue one must always use the following:

<cfobject type="Java" action="create" name="File" class="java.io.File">
<cfset variables.lockFile =variables. File.init(JavaCast("string","c:/tmp.1")) >

This code works fine. Certainly it is a good practice to use variables with explicitly defined scope just for performance improvement, but in this particular example it is absolutely required.
Another work around is to declare 'File' as a local function variable and then explicitly set the component variable after class instantiation.
macromicah said on Jan 25, 2005 at 9:56 PM :
why does the example use weblogic instead of jrun? should it show examples using macromedia products?
No screen name said on Jan 26, 2005 at 10:40 AM :
teh follwing code give me an error

<cfobject action = "Create" type="Java" class="APSEManager" name="myObj">

"Class not found: AmandaDev/APSEManager"
the APSEManager file is in the same folder where my cfm form, which call it, is.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-pb9.htm