javascript - Java Nashorn实例方法

标签 javascript java jvm

以为我会尝试Nashorn脚本功能。一切正常,而且我似乎可以从.js脚本中调用静态方法,如下所示:

var fun3 = function() {
    print("Another day!");
    var Go = Java.type('somePack.Go');
    var result = Go.fun1('John Doe');
    print("\n" + result);
}


在我的“ Go” java类中:

public static String fun1(String name) {
    System.out.format("Hi there from Java, %s", name);
    return "greetings from java";
}


但是我想做的是使fun1方法成为非静态的,以使其成为实例化方法。

   public String fun1(String name) {
        System.out.format("Hi there from Java, %s", name);
        return "greetings from java";
    }


在我的script.js中,我确实尝试过:

var Go = new Java.type('somePack.Go');


但是我得到了错误:

Caused by: <eval>:19 TypeError: function type() { [native code] } is not a constructor function


有没有简单的方法可以实现我的目标?

谢谢

全栈 :

    at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:470)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:392)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeFunction(NashornScriptEngine.java:190)
    at somePack.Go.go(Go.java:35)
    at somePack.Go.main(Go.java:16)
Caused by: <eval>:19 TypeError: function type() { [native code] } is not a constructor function
    at jdk.nashorn.internal.runtime.ECMAErrors.error(ECMAErrors.java:57)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:213)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:185)
    at jdk.nashorn.internal.runtime.ECMAErrors.typeError(ECMAErrors.java:172)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.getBestConstructor(ScriptFunctionData.java:244)
    at jdk.nashorn.internal.runtime.ScriptFunction.findNewMethod(ScriptFunction.java:758)
    at jdk.nashorn.internal.runtime.ScriptObject.lookup(ScriptObject.java:1827)
    at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:104)
    at jdk.nashorn.internal.runtime.linker.NashornLinker.getGuardedInvocation(NashornLinker.java:98)
    at jdk.internal.dynalink.support.CompositeTypeBasedGuardingDynamicLinker.getGuardedInvocation(CompositeTypeBasedGuardingDynamicLinker.java:176)
    at jdk.internal.dynalink.support.CompositeGuardingDynamicLinker.getGuardedInvocation(CompositeGuardingDynamicLinker.java:124)
    at jdk.internal.dynalink.support.LinkerServicesImpl.getGuardedInvocation(LinkerServicesImpl.java:154)
    at jdk.internal.dynalink.DynamicLinker.relink(DynamicLinker.java:253)
    at jdk.nashorn.internal.scripts.Script$Recompilation$2$384$\^eval\_.fun3(<eval>:19)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:637)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:494)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:393)
    at jdk.nashorn.api.scripting.ScriptObjectMirror.callMember(ScriptObjectMirror.java:199)
    at jdk.nashorn.api.scripting.NashornScriptEngine.invokeImpl(NashornScriptEngine.java:386)

最佳答案

你应该用

var Go = Java.type('somePack.Go');
var goObject = new Go();

关于javascript - Java Nashorn实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779508/

相关文章:

java - Swing 控件上的 block 更改操作

java - Google Calendar API(Java)-创建Google Calendar Event时的时间格式无效

java - 如何识别 Java 字节码中的覆盖方法?

java - Docker 和设置 JVM 参数

macos - Mac OS X Java 更新到 1.5u7 是否重击了我的 1.6.0 laf.jar?

javascript - Knockout View 模型更新时未更新 Kendo UI 下拉菜单

javascript - 更改按自定义属性排序的对象数组的顺序

java - jQuery Ajax 错误解决

javascript - 在React组件安装到DOM后触发了任何事件吗?

javascript - 文本字段为大写,但句子大小写占位符?