将命令行开关传递给Nashorn JavaScript引擎

时间:2016-08-12 09:58:50

标签: java javafx-8 nashorn

我正在尝试使用nashorn在Java环境中运行以下代码。Code Doc

load("fx:base.js");
load("fx:controls.js");
load("fx:graphics.js");

var material = new PhongMaterial();
material.diffuseColor = Color.LIGHTGREEN;
material.specularColor = Color.rgb(30, 30, 30);

var meshView = Java.to([
    new Box(200, 200, 200),
    new Sphere(100),
    new Cylinder(100, 200)
], "javafx.scene.shape.Shape3D[]");

for (var i = 0; i != 3; i++) {
    meshView[i].material = material;
    meshView[i].translateX = (i + 1) * 220;
    meshView[i].translateY = 200;
    meshView[i].translateZ = 20;
    meshView[i].drawMode = DrawMode.FILL;
    meshView[i].cullFace = CullFace.BACK;
};

var pointLight = new PointLight(Color.WHITE);
pointLight.translateX = 800;
pointLight.translateY = -200;
pointLight.translateZ = -1000;

var root = new Group(meshView);
root.children.add(pointLight);

var scene = new Scene(root, 800, 400, true);
scene.fill = Color.rgb(127, 127, 127);
scene.camera = new PerspectiveCamera(false);
$STAGE.scene = scene;
$STAGE.show();

它使用JavaScript API创建JavaFx场景。

如果将jjs命令行实用程序与-fx开关一起使用,它将按预期工作,但如果您通过Java代码使用nashorn脚本引擎执行相同的文件,则会引发以下异常

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at jdk.nashorn.internal.runtime.Context.findClass(Context.java:983)
    at jdk.nashorn.internal.objects.NativeJava.simpleType(NativeJava.java:489)
    at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:320)
    at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:312)
    at jdk.nashorn.internal.objects.NativeJava.type(NativeJava.java:308)
    at jdk.nashorn.internal.scripts.Script$Recompilation$3$500A$\=fx\!base.LOAD_FX_CLASSES(fx:base.js:38)
    at jdk.nashorn.internal.scripts.Script$4$\=fx\!controls.:program(fx:controls.js:30)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
    at jdk.nashorn.internal.runtime.Context.evaluateSource(Context.java:1150)
    at jdk.nashorn.internal.runtime.Context.load(Context.java:799)
    at jdk.nashorn.internal.objects.Global.load(Global.java:995)
    at jdk.nashorn.internal.scripts.Script$\^eval\_.:program(<eval>:2)
    at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:636)
    at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:229)
    at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:387)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:437)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:401)
    at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:397)
    at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:147)
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:212)
    at com.metalop.nashorn.javafx.GettingStarted.main(GettingStarted.java:23)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
    ... 25 more

那么如何在nashorn的java代码中传递-fx开关或初始化javafx?

2 个答案:

答案 0 :(得分:0)

使用jdk.nashorn.api.scripting.NashornScriptEngineFactory实例化您的Nashorn引擎。它有getScriptEngine的重载,它带有一个字符串数组 - 这些是命令行参数。

很遗憾,您无法通过javax.script界面执行此操作,但jdk.nashorn.api.scripting也是公开且受支持的API。

答案 1 :(得分:0)

正如Attila所说,无法通过javax.script传递参数。你有两个选择:

(1)使用Nashorn特定API - &gt; https://docs.oracle.com/javase/8/docs/jdk/api/nashorn/jdk/nashorn/api/scripting/NashornScriptEngineFactory.html

(或)

(2)在java命令行中定义“nashorn.args”系统属性。

另请参阅:https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes

在任何一种情况下,我都不确定具体的选项,“ - fx”会起作用。这是因为FX代码具有特定的初始化要求,这些要求由“jjs”工具处理。不确定这是否适用于脚本引擎 嵌入模式。