我正嘗試在使用nashorn的Java環境中運行以下代碼。 Code Doc通過命令行切換到Nashorn JavaScript引擎
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場景。
如果您使用-fx
開關它能正常工作,但如果執行通過Java代碼犀牛腳本引擎相同的文件,它會引發以下異常
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
那麼,如何做jjs
命令行實用程序我通過-fx
開關或在java代碼中初始化javafx for nashorn?
我認爲關鍵部分是根'IllegalStateException:Toolkit未初始化'異常。這意味着你正在運行JavaFX代碼而沒有啓動JavaFX('javafx.application.Application')。所以一個解決方案應該是用'javafx.application.Application'運行你的java應用程序,而不是任何'Main'類。 –
@NikosParaskevopoulos:從Application擴展類確實會加載依賴關係並解決問題。感謝您的評論...! – Shiva