Skip to main content
Participating Frequently
March 20, 2008
Question

Using java objects in coldfusion

  • March 20, 2008
  • 4 replies
  • 1843 views
I have a java object, that needs to be used in a cfm page. The java object itself references classes from an external jar file. I've copied the java object class, and the external jar files in the web-inf/lib directory of coldfusion.

My java object is:
--------------------------------------------------------------------------------
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
public class TestObj{

public TestObj() {
// TODO Auto-generated constructor stub
}

public boolean Connect(String url)
{
boolean connected=true;
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(0, false));
try {
// Execute the method.
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
connected=false;
}
connected=true;
} catch (Exception e) {
connected=false;
} finally {
// Release the connection.
method.releaseConnection();
}
return(connected);
}
}
-----------------------------------------------------------------------

I'm accessing the object in the following way within the cfm:
----------------------
<cfobject action="create" type="Java" class="TestObj" name="tobj">
<cfset connctd=tobj.Connect(" http://some url...")>
----------------------

I get an error '500 null' when I load this cfm. When I comment out the code for httpclient and related objects in TestObj and reload the page, I don't see an error. This makes me think that httpclient is not accessible to the java object in the coldfusion environment even though I've copied the related jar files for httpclient and the other objects in the web-inf/lib directory.

So my question is: Can a java object, being called from a coldfusion page, reference other java objects available in jar files in a coldfusion environment?

Any help on this is greatly appreciated.

Thanks
This topic has been closed for replies.

4 replies

Inspiring
March 20, 2008
Yes, the logging jars often cause problems. Glad it is working now!
Participating Frequently
March 20, 2008
If you put a main method in your TestObj class, set your CLASSPATH to include the TestObj jar file and dependent jars, and try to run it, does it run?

Try this main method:

public static void main(String[] args) {
TestObj tobj = new TestObj();
tobj.Connect(" http://www.adobe.com/");
}
Inspiring
March 20, 2008
When you checked the CF logs, what was the detailed error message? As I said, I suspect the problem may be a conflict with CF's version of the logging jars.

Btw, since you put the objects in web-inf/lib your test class is contained in a jar, correct?
Participating Frequently
March 20, 2008
Yes, my test class is in a jar.

Here's the error I'm seeing in exception.log:
----------------------------
"Error","jrpp-2"...

java.lang.NoClassDefFoundError
at TestObj.Connect(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:74)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:1634)
at cfplayer2ecfm1189192416._factor6(C:\Webserver\player\player.cfm:157)
at cfplayer2ecfm1189192416.runPage(C:\Webserver\player\player.cfm:1)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:152)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:349)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:225)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:51)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:86)
at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:69)
at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:52)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:115)
at coldfusion.CfmServlet.service(CfmServlet.java:107)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:78)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:257)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:541)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:204)
at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:318)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:426)
at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:264)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
---------------------
Inspiring
March 20, 2008
Is that the entire message? What version of CF are you using?
Inspiring
March 20, 2008
ashishpomal wrote:
> So my question is: Can a java object, being called from a coldfusion page,
> reference other java objects available in jar files in a coldfusion environment?

I am not speaking with authority here, I've done just a bit of Java and
ColdFusion, but I believe so. My first thought is since you moved the
associated files are the paths now wrong?

> import org.apache.commons.httpclient.*;
> import org.apache.commons.httpclient.methods.*;
> import org.apache.commons.httpclient.params.HttpMethodParams;

Are these not looking for these jars in some
/org/apache/commons/httpclient/ directory? If you have moved them to
the web-inf/lib directory is this not a different path and would it not
need to be reflected in the above import statements?
Inspiring
March 20, 2008
First, did you verify the class runs from java? I think http client has a dependency on commons codec. So it would not work without it.

public static void main(String[] args) {
TestObj t = new TestObj();
boolean result = t.Connect(" http://www.google.com");
System.out.println(result);
}

Second, a common problem with the commons jars (no pun intended) is conflicts with the logging jars CF uses. So that might be part of the problem. Check the CF logs to get more information about the 500 error. If the problem is a conflict with the CF logging jars, try putting the class in a jar and using the javaLoader.cfc to instantiate it.

Participating Frequently
March 20, 2008
The class runs from my java environment. I've copied the dependancy jars (logging and codec besides the httpclient itself) in the web-inf/lib dir.