Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ECMA TypeError calling Java class from Worklight adapter

I've seen a number of questions about this issue but no conclusive answers. I am having trouble calling a Java class from my Worklight adapter implementation. I replaced my code with the code from the IBM Worklight Java Adapter tutorial and it fails in the exact same way. Furthermore I found a response on IBM's site saying the Java 1.7 compiler might cause this problem and to use Java 1.6 instead. I validated that my compiler in Eclipse is Java 1.6.

Screenshot of Eclipse and code path

My Java classes all begin with com (e.g. com.worklight.customcode). I've tried both calling public static methods (using the proper syntax) as well as instantiating the object and calling the method. As mentioned above, I've also validated I'm using the Java 1.6 compiler.

Here are some code samples:

Adapter implementation file:

function addTwoIntegers(a,b){
    return {
        result: com.worklight.customcode.Calculator1.addTwoIntegers(a,b)
    };
}

Java file (unedited IBM Worklight sample): package com.worklight.customcode;

import java.util.logging.Logger;

public class Calculator1 {

    private final static Logger logger = Logger.getLogger(Calculator1.class.getName());

    public static int addTwoIntegers(int first, int second){
        logger.info("addTwoIntegers invoked");
        return first + second;
    }

    public int subtractTwoIntegers(int first, int second){
        logger.info("subtractTwoIntegers invoked");
        return first - second;
    }

}

Error in the console:

TypeError: Cannot call property addTwoIntegers in object [JavaPackage com.worklight.customcode.Calculator1]. It is not a function, it is "object". (%2FUsers%2Fhome%2Fdev%2Fapp%2Fappprj%2Fadapters%2Fadapter/adapter-impl.js#26) FWLSE0101E: Caused by: null

Some of the related questions are:

  • Ecma Error: TypeError: Cannot call property
  • i want to call a java class from the worklight adapter
  • https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014867695
like image 467
user2217751 Avatar asked Oct 05 '22 02:10

user2217751


1 Answers

I was close, it wasn't CLASSPATH per se, but rather (apparently) Eclipse project settings.

After a week or more of chasing this off and on, I edited the .project file to include certain buildCommand tags that my project didn't have. Adding the following buildCommands to the section allowed my code to launch Java classes from JavaScript after restarting Eclipse.

<buildSpec>
    <buildCommand>
        <name>org.eclipse.jdt.core.javabuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.wst.common.project.facet.core.builder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>com.worklight.studio.plugin.WorklightProjectBuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
    <buildCommand>
        <name>org.eclipse.wst.validation.validationbuilder</name>
        <arguments>
        </arguments>
    </buildCommand>
</buildSpec>

The .project file is located in the root of the Worklight project home (e.g. myproject/.project). I figured this out eventually, by running across a working project that did successfully call Java from JavaScript.

See http://www.ibm.com/developerworks/rational/library/server-side-mobile-application-development-1/

I copied the code verbatim to my project and it had the same behavior as my code. I copied my code to that project and my code worked (!!). I then compared the classpaths, which were somewhat different, but it didn't change the behavior. I inspected the .project file and noticed my file didn't have the buildCommand tags above. Instead my file had a number of externalToolBuilding tags, presumably because one of the guys on my team uses an IDE other than Eclipse and his .project became the one in the project. (I think it's Sublime, if it matters).

I don't quite understand all the details of what each of these tags do or exactly how and why Worklight and Eclipse change their behavior because of it (or why they disappeared in the first place). However, it made my code work. It only cost me a week's worth of work (ack!).

I hope this helps someone else in the future.

like image 174
user2217751 Avatar answered Oct 10 '22 02:10

user2217751