2017-10-16 16 views
1

Je pensais essayer la fonctionnalité de script Nashorn. Tout fonctionne très bien, et je semble pouvoir appeler des méthodes statiques de mon script .js comme ceci:Méthode instanciée Java Nashorn

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

Inside my "Go" classe java:

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

Mais ce que je voudrais faire est de rendre la méthode fun1 non statique, pour en faire une méthode instanciée.

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

Dans mes script.js J'ai essayé:

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

Mais je reçois l'erreur:

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

est-il un moyen facile de réaliser ce que je suis après?

Merci

pile complète:

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) 

Répondre

1

Vous devez utiliser

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

var = new goObject (Java.type ('somePack.Go'))(); aurait travaillé aussi bien mais ... – wickund