wax_lyrical
wax_lyrical

Reputation: 942

Java Nashorn instanced method

Thought I'd try out the Nashorn scripting functionality. Everything is working fine, and I seem to be able to call static methods from my .js script like so:

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

Inside my "Go" java class:

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

But what I would like to do is the make the fun1 method non-static, to make this an instanced method.

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

In my script.js I did try:

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

But I get the error:

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

Is there an easy way to achieve what I'm after?

Thanks

Full stack :

    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)

Upvotes: 1

Views: 218

Answers (1)

wickund
wickund

Reputation: 1177

You should use

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

Upvotes: 2

Related Questions