13 Replies Latest reply on Mar 20, 2008 12:46 PM by -==cfSearching==-

    Using java objects in coldfusion

    ashishpomal
      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
        • 1. Re: Using java objects in coldfusion
          Level 7
          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?
          • 2. Re: Using java objects in coldfusion
            -==cfSearching==- Level 4
            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.

            • 3. Re: Using java objects in coldfusion
              ashishpomal Level 1
              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.
              • 4. Re: Using java objects in coldfusion
                ashishpomal Level 1
                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.
                • 5. Re: Using java objects in coldfusion
                  -==cfSearching==- Level 4
                  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?
                  • 6. Re: Using java objects in coldfusion
                    Kronin555 Level 1
                    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.
                    • 7. Re: Using java objects in coldfusion
                      ashishpomal Level 1
                      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:2 8)
                      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)
                      ---------------------
                      • 8. Re: Using java objects in coldfusion
                        ashishpomal Level 1
                        Yes...I restarted coldfusion after copying the jar files.
                        • 9. Re: Using java objects in coldfusion
                          -==cfSearching==- Level 4
                          Is that the entire message? What version of CF are you using?
                          • 10. Re: Using java objects in coldfusion
                            ashishpomal Level 1
                            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.
                            • 11. Re: Using java objects in coldfusion
                              Kronin555 Level 1
                              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/");
                              }
                              • 12. Re: Using java objects in coldfusion
                                ashishpomal Level 1
                                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 !!
                                • 13. Re: Using java objects in coldfusion
                                  -==cfSearching==- Level 4
                                  Yes, the logging jars often cause problems. Glad it is working now!