5
votes

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:

5

5 Answers

2
votes

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.

2
votes

we had that problem several times and it always had something to do with a corrupted eclipse .project file. What happens is that the Java Class you have does not get built and does not get added to your worklight.war file. When you deploy your app, the compiled class is missing on the server and the ECMA error tells you that in a very cryptic way.

One solution we found was to open the properties of the WL project with a right click go to the Java Build Path and move some of the entries UP and DOWN using the buttons there. After closing the properties dialog eclipse should rewrite the .project file and the build should work.

Another thing we did sometimes was to add a new class to the project/server/java part of the project using the eclipse New-Class wizard, clean and rebuild the project and then remove the class again. Maybe even start eclipse with the option -clean at the end of the startup string that it uses.

0
votes

First lets try to make sure it is not a Type conversion problem. In your Java code define the Add function with two object parameters:

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

Look at the server output. If it was a Type issue, the method should now work. Otherwise, we have a different issue...

0
votes

I could not get this same example working because of this error, despite trying all of these things mentioned above. I'm using Eclipse 4.4, Worklight 6.2, WebSphere Application Server 8.5.5.1, Java 1.7. I was running the adapter by right-clicking it and selecting Run As > Invoke Worklight Procedure.

In the end, I got it working by explicitly adding the generated Adapters.war to the project build path. From the build path properties: Add JARs... > Adapters/bin/Adapters.war. Only then did it start working.

0
votes

This problem often happens to me even when I open new projects on new workspaces. I believe that the solutions proposed here do not cover all the cases, so for the usefulness of all, I list here all the possible solutions I found to solve the problem:

1) check that the Java version of the running server is the same of the project, as mentioned here. I personally remove all the other installed JRE in the workspace properties. I found that in some cases they should share also the same minor version, for example if the server runs the 1.7.0 you shouldn't have the 1.7.49

2) check that also the compiler compliance level of the workspace fits the same java version run by the server, as mentioned here. This is what causes me the most of troubles since it is something that I forgot often.

3) check the .project file: as mentioned here

4) Usually with this 3 hint my problems are gone, but for the sake of completeness I add also this hint that could have some sense

5) finally you can also need to remove the previous "malformed" application from server and rebuild the WAR