Frage

I'm trying to call a static method on a class from JavaScript executed by Nashorn:

var hooks = new JavaImporter(eu.wansdyke.carbon.hooks);
with (hooks) {
  Archiver.archive(bucket, key, existingValue, 1);
}

Unfortunately I'm getting this error:

javax.script.ScriptException: ReferenceError: "eu" is not defined in  at line number 2

The class that invokes Nashorn is in a eu.wansdyke... package so I'm a bit surprised at this. Is there something I'm doing wrong?

War es hilfreich?

Lösung

If you access it through the Packages object, I think it will work.

For instance:

Packages.eu.wansdyke.carbon.hooks

For referenced pull it from the : Nashorn Docs

Andere Tipps

The cause for your problem is that Nashorn loads the java.*, javax.*, javafx.*, com.*, edu.* and org.* packet hierarchies into global scope, but not eu.*. That means you can not reference that package directly.

But you should be able to import individual classes from the eu.* hierarchy with Java.type(String).

 var Archiver = Java.type("eu.wansdyke.carbon.hooks.Archiver");
 Archiver.archive(bucket, key, existingValue, 1);

More of a workaround than a satisfactory answer, but I've been able to set an instance of the Archiver class as a global variable on the JS runtime using void javax.script.ScriptEngine.put(String key, Object value). A static method didn't seem to then be available so I've used an instance one and called a static method from it.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top