• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using java objects in coldfusion

New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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
TOPICS
Advanced techniques

Views

1.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

Did you restart coldfusion after you copied the jar files into WEB-INF/lib of coldfusion? Go into the cfadministrator and verify that the jar files you copied over are listed in the classpath (under "Settings Summary").

Yes, java classes can reference external jar files, but those jar files must be in Coldfusion's classpath.

Look at CF's log files. There has to be an error message somewhere. Turn on debugging (if it isn't already) and try again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

Yes...I restarted coldfusion after copying the jar files.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

Ok. The problem is resolved.

I think there was a conflict with the commons-logging jar files. There was an existing commons-logging.jar, and I'd copied a newer version of commons-logging jar file as well. I cleaned up the jar files, and copied commons-logging, commons-codec, and commons-httpclient jars into the coldfusion lib directory, along with my Test object jar in the same directory. Restarted coldfusion, and everything works fine.

Thanks all for your help !!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

The jars that contain the packages and classes specified by the import statements are available in the web-inf/lib directory. So when the import statements are encountered, the specific classes should be accessed from these jars.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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)
---------------------

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

Is that the entire message? What version of CF are you using?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

Yes. That's the message I'm seeing. I have debugging on, but the page does not show anything else besides the '500 null'. The message is shown in exception.log.

I'm using Coldfusion MX 7.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

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/");
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Mar 20, 2008 Mar 20, 2008

Copy link to clipboard

Copied

LATEST
Yes, the logging jars often cause problems. Glad it is working now!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation