Search code examples
javaibm-mobilefirstworklight-adapters

worklight adapter call java got Ecma Error ReferenceError


Seems the error is related to package of the java class, if it begin with com.xx.xx, it works, if it begin with cn.xx.xx, the error will throw. is this a bug?

The Java Class:

package cn.linkey.util;

public class TestJava {

    public String md5(String aa){
        return "md5" + aa;
    }

}

the Adapter:

function sayHello(name){
    var tj = new cn.linkey.util.TestJava();

    return {result: "hello " + tj.md5(name) + '.'};
}

the Error:

{
       "errors": [
      "Ecma Error: ReferenceError: \"cn\" is not defined. (TestHTTP-impl.js#41)"
   ],
   "info": [
   ],
   "isSuccessful": false,
   "warnings": [
   ]
}

Server error:

[ERROR   ] FWLSE0099E: An error occurred while invoking procedure  [project     HelloWL]TestHTTP/sayHelloFWLSE0100E:  parameters: [project HelloWL]
ReferenceError: "cn" is not defined. (TestHTTP-impl.js#41)
FWLSE0101E: Caused by:  [project HelloWL]nullorg.mozilla.javascript.EcmaError: ReferenceError:     "cn" is not defined. (TestHTTP-impl.js#41)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
        at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:3750)
         at org.mozilla.javascript.ScriptRuntime.name(ScriptRuntime.java:1728)
        at org.mozilla.javascript.gen.TestHTTP_impl_js_6._c_sayHello_1(TestHTTP-impl.js:41)
        at org.mozilla.javascript.gen.TestHTTP_impl_js_6.call(TestHTTP-impl.js)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen.TestHTTP_impl_js_6.call(TestHTTP-impl.js)
        at com.worklight.integration.js.JavaScriptManager.callFunction(JavaScriptManager.java:240)
        at com.worklight.integration.js.JavaScriptManager.invokeFunction(JavaScriptManager.java:214)
        at com.worklight.integration.js.JavaScriptManager.invokeFunction(JavaScriptManager.java:194)
        at com.worklight.integration.services.impl.AdapterManagerImpl.invokeFunction(AdapterManagerImpl.java:117)
        at com.worklight.integration.js.JavaScriptProcedureInvoker.invoke(JavaScriptProcedureInvoker.java:42)
        at com.worklight.integration.model.ProcedureInvoker.invokeProcedure(ProcedureInvoker.java:57)
        at com.worklight.integration.model.Procedure.invoke(Procedure.java:166)
        at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:169)
        at com.worklight.integration.model.InvocationContext.call(InvocationContext.java:38)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at com.worklight.server.util.ProjectLocal$1RunnableWrapper.run(ProjectLocal.java:261)
        at java.lang.Thread.run(Thread.java:695)

Solution

  • The Worklight documentation specifically mentions to use only com/org/net as the package prefix, whereas you are using cn.

    See Slide #7 in the Using Java in adapters training module.
    Change the used prefix as that will likely fix the issue you are experiencing.