1

I'm working on implementing Amazon Pay, and instead of converting one of the SDKs to CF, figured I would just use the java sdk - but, I haven't ever worked with anything that is not part of the java core install. Any ideas why this

createObject('java', 'com.amazon.pay.api.WebstoreClient')

is not working ( Object instantiation exception. Error: org/json/JSONException.) when I drop the jar in cfusion\wwwroot\web_inf\lib.

the jar file came from here: https://github.com/amzn/amazon-pay-api-sdk-java/releases

I suspect that the issue is with the import statements for the "core" libraries, but don't know why CF wouldn't be able to find them (seems pretty basic) so I must be doing something wrong.

Windows 10 dev box running CF2016, dev install with a single instance, both restarted and rebooted.

ANSWERED BY @SOS IN THE COMMENTS - reposted here.

  1. Downloaded the sdk to c:/path/to/lib/amazon-pay-api-2.5.1.jar.
  2. Added
this.javaSettings = { 
    LoadPaths = [ "c:/path/to/lib/amazon-pay-api-2.5.1.jar"], 
    loadColdFusionClassPath=true };
  1. Restarted Application.cfc

Edited to add code and complete error

This is the complete contents of the file that errors. I have tried both ways and get the same error - 
<!--- <cfobject action="create" type="java" class="com.amazon.pay.api.WebstoreClient" name="WebstoreClient"> --->

<cfset wsClient=createObject('java', 'com.amazon.pay.api.WebstoreClient')>

<cfdump var="#wsClient#">

The error

Object instantiation exception.
An exception occurred while instantiating a Java object. 
Error: org/json/JSONException.
 
The error occurred in E:/webroot/striking/d.cfm: line 2
1 : <!--- <cfobject action="create" type="java" class="com.amazon.pay.api.WebstoreClient" name="WebstoreClient"> --->
2 : <cfset wsClient=createObject('java', 'com.amazon.pay.api.WebstoreClient')>
3 : 
4 : <cfdump var="#wsClient#">
Resources:
Check the ColdFusion documentation to verify that you are using the correct syntax.
Search the Knowledge Base to find a solution to your problem.
Browser     Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36
Remote Address      127.0.0.1
Referrer    
Date/Time   27-May-22 10:20 AM
Stack Trace
at cfd2ecfm486526222.runPage(E:/webroot/striking/d.cfm:2) at cfd2ecfm486526222.runPage(E:/webroot/striking/d.cfm:2)

java.lang.NoClassDefFoundError: org/json/JSONException
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Class.java:2583)
    at java.lang.Class.privateGetPublicFields(Class.java:2614)
    at java.lang.Class.getFields(Class.java:1557)
    at coldfusion.runtime.java.ObjectHandler.Initialize(ObjectHandler.java:35)
    at coldfusion.runtime.java.ObjectHandler.<init>(ObjectHandler.java:30)
    at coldfusion.runtime.java.ReflectionCache.get(ReflectionCache.java:47)
    at coldfusion.runtime.java.JavaProxy.<init>(JavaProxy.java:35)
    at coldfusion.runtime.java.JavaProxyFactory.getProxy(JavaProxyFactory.java:121)
    at coldfusion.runtime.ProxyFactory.getProxy(ProxyFactory.java:68)
    at coldfusion.runtime.CFPage.createObjectProxy(CFPage.java:6608)
    at coldfusion.runtime.CFPage.CreateObject(CFPage.java:6571)
    at coldfusion.runtime.CFPage.CreateObject(CFPage.java:6505)
    at coldfusion.runtime.CFPage.CreateObject(CFPage.java:6431)
    at coldfusion.runtime.CFPage.CreateObject(CFPage.java:6372)
    at cfd2ecfm486526222.runPage(E:\webroot\striking\d.cfm:2)
    at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:251)
    at coldfusion.tagext.lang.IncludeTag.handlePageInvoke(IncludeTag.java:737)
    at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:573)
    at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
    at coldfusion.filter.IpFilter.invoke(IpFilter.java:45)
    at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:505)
    at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:43)
    at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
    at coldfusion.filter.PathFilter.invoke(PathFilter.java:153)
    at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:30)
    at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:94)
    at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:78)
    at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
    at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
    at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:60)
    at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
    at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
    at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
    at coldfusion.CfmServlet.service(CfmServlet.java:219)
    at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at coldfusion.inspect.weinre.MobileDeviceDomInspectionFilter.doFilter(MobileDeviceDomInspectionFilter.java:121)
    at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at com.seefusion.Filter.doFilter(Filter.java:109)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at com.seefusion.SeeFusionValve.invoke(SeeFusionValve.java:52)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:536)
    at org.apache.coyote.ajp.AbstractAjpProcessor.process(AbstractAjpProcessor.java:898)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
    ... 68 more

Regards,

M

MikeG
  • 107
  • 1
  • 8
  • An alternative to this approach would be to use Swagger to generate your own API client with the [Amazon Pay Swagger API definition](https://amazon-pay-api-models.s3.eu-west-1.amazonaws.com/index.html). – Debbie Martindale May 26 '22 at 20:04
  • @DebbieMartindale - well that's a new one to me, but I was hoping to not have to write one in CF and just leverage the java one. This will def help with PlanB - TYVM – MikeG May 26 '22 at 21:03
  • Could you post a link to the java example you're basing your CF code on? Also, please post A) the complete CF code you're using and B) the full stack trace message so we can try and reproduce the error – SOS May 27 '22 at 12:40
  • @SOS Edited original post to include the info. – MikeG May 27 '22 at 15:31
  • That library has some [dependencies](https://github.com/amzn/amazon-pay-api-sdk-java/blob/master/pom.xml). According to the error message CF can't find one of them, i.e. [org.json version 20210307]](https://github.com/stleary/JSON-java/releases/tag/20210307) . Try adding that library to your class path too (must restart the CF service afterward for the lib to be detected.) – SOS May 27 '22 at 17:08
  • ... Though there's a much easier method of loading java libraries. Instead of copying the jar files into web-inf/lib and restarting CF, use the Application.cfc setting `this.javaSettings`. For more info, see https://helpx.adobe.com/coldfusion/developing-applications/using-web-elements-and-external-objects/integrating-jee-and-java-elements-in-cfml-applications/enhanced-java-integration-in-coldfusion.html – SOS May 27 '22 at 17:13
  • @SOS Yes, I got that it had dependencies, but since I don't have any experience with Java, assumed that the JSON was part of the core, installed, java that CF was running. Apparently that is not the case; so much for "the easy way". I do know about `this.javaSettings`, but this was/is a quick start and I was keeping it simple. – MikeG May 27 '22 at 18:15
  • `so much for "the easy way".` What's the issue with downloading another jar or two? I'd guess most of the libs in dependency list are already bundled in CF (apache httpclient, commons-lang, etc..), but you'll have to test the code and see. Would help to see a link to the java example you're basing this on btw – SOS May 27 '22 at 20:12
  • @SOS ` What's the issue with downloading another jar or two?` Until I just read that I had zero idea that it might be that simple. As said earlier, I have ZERO experience with java, other than reading docs and using available core classes. This is all new ground. Given a choice between "learning Java in a week" or refactoring these class files into a cfc, you see why I say `so much for the easy way`. At the very least, you gave me a place to start with this off the cuff comment `already bundled in CF (apache httpclient, comm..` Obvious to you, not to me, the noob fishing for a place to start – MikeG May 28 '22 at 04:28
  • *I was keeping it simple* TBH, using `this.javaSettings` would be easier. I just tried it and the createObject() worked out of the box. Steps: 1) Downloaded the sdk to c:/path/to/lib/amazon-pay-api-2.5.1.jar. 2) Added this.javaSettings: `this.javaSettings = { LoadPaths = [ "c:/path/to/lib/amazon-pay-api-2.5.1.jar"], loadColdFusionClassPath=true };` 3) Restarted Application.cfc 4) Ran code `webClient = createObject('java', 'com.amazon.pay.api.WebstoreClient'); writeDump(webClient);` – SOS May 28 '22 at 21:40
  • 1
    Thank you, thank you, thank you, and I apologize for letting my frustration getting in the way of my common sense, I'm kinda embarrassed that I did not try this when you first suggested it . That snippet works just fine, though the CFDUMP throws and exception (even though it displays the dump) It also works with or without this statement. loadColdFusionClassPath=true – MikeG May 29 '22 at 02:31
  • (Update) Hm.... it didn't throw any errors for me. Did you remove all jars added from web-inf/lib? If not, do that first, then restart CF and try again. Also, are you loading any other jars besides the sdk in your application.cfc? Because `loadColdFusionClassPath=false` should *not* work. The reason is this.javaSettings loads the sdk jar in it's own separate class loader, which isn't able access to the other jars bundled with CF (like apache httpclient, etc...) unless you explicitly tell it to load that directory i.e. {cfusion_root/lib} too by using loadColdFusionClassPath=true. – SOS May 30 '22 at 02:03
  • (Update) FWIW, I just retested and confirmed it does fail when loadColdFusionClassPath=false. Though with a slightly different error: java.lang.NoClassDefFoundError: org/apache/http/HttpResponse. So something must be different on your end. Be sure to restart the Application.cfc in between tests, or it won't pick up the change in the loadColdFusionClassPath setting. – SOS May 30 '22 at 20:34

0 Answers0