ECMA TypeError从Worklight适配器调用Java类 [英] ECMA TypeError calling Java class from Worklight adapter

查看:152
本文介绍了ECMA TypeError从Worklight适配器调用Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于这个问题的问题,但没有确凿的答案。我无法从Worklight适配器实现中调用Java类。我用IBM Worklight Java Adapter教程中的代码替换了我的代码,它以完全相同的方式失败。此外,我在IBM的网站上发现了一个回应,称Java 1.7编译器可能会导致此问题并使用Java 1.6。我验证了Eclipse中的编译器是Java 1.6。

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.

我的Java类都以com开头(例如com.worklight.customcode)。
我试过调用公共静态方法(使用正确的语法)以及实例化对象并调用方法。如上所述,我还验证了我正在使用Java 1.6编译器。

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.

以下是一些代码示例:

适配器实现文件:

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

Java文件(未编辑的IBM Worklight示例):
package com。 worklight.customcode;

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;
    }

}

控制台出错:


TypeError:无法在对象[JavaPackage
com.worklight.customcode.Calculator1]中调用属性addTwoIntegers。它不是一个函数,它是
对象。
(%2FUsers%2Fhome%2Fdev%2Fapp%2Fappprj%2Fadapters%2Fadapter /适配器-impl.js#26)
FWLSE0101E:空

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

一些相关的问题是:

  • 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

推荐答案

我很接近,它本身不是CLASSPATH,而是(显然)Eclipse项目设置。

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

经过一周或更长时间的追逐,我编辑了.project文件,以包含我的项目没有的某些buildCommand标签。将以下buildCommands添加到该部分允许我的代码在重新启动Eclipse后从JavaScript启动Java类。

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>

.project文件位于Worklight项目主目录的根目录中(例如myproject / .project) 。我最终通过运行从JavaScript成功调用Java的工作项目来解决这个问题。

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.

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

我将代码逐字复制到我的项目中,它与我的代码具有相同的行为。我将我的代码复制到该项目,我的代码工作(!!)。然后我比较了类路径,它们有些不同,但它没有改变行为。我检查了.project文件,发现我的文件上面没有buildCommand标签。相反,我的文件有许多externalToolBuilding标签,大概是因为我团队中的一个人使用的是Eclipse以外的IDE,而他的.project成为项目中的一个。 (我认为这是崇高的,如果重要的话)。

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).

我不太了解每个标签的所有细节,或者究竟是如何以及为什么Worklight和Eclipse改变了它们的行为(或者为什么它们消失了首先)。但是,它使我的代码工作。它只花了我一周的工作量(ack!)。

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.

这篇关于ECMA TypeError从Worklight适配器调用Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆