2016-08-12 59 views
0

我正嘗試在使用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?

+1

我認爲關鍵部分是根'IllegalStateException:Toolkit未初始化'異常。這意味着你正在運行JavaFX代碼而沒有啓動JavaFX('javafx.application.Application')。所以一個解決方案應該是用'javafx.application.Application'運行你的java應用程序,而不是任何'Main'類。 –

+0

@NikosParaskevopoulos:從Application擴展類確實會加載依賴關係並解決問題。感謝您的評論...! – Shiva

回答

0

有幾件事情,我做了JavaScript的工作,因爲傳遞-fx nashorn arg沒有爲我工作。

  1. 正如尼科斯在評論中提到的類必須從javafx.application.‌​Application延伸,然後主要功能應調用這對於犀牛初始化和JS腳本執行的代碼launch()

  2. 啓動方法有start(Stage primaryStage)簽名。評估時,必須將Stage對象綁定到JavaScript。

以下代碼是我如何實現它的示例。

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import javax.script.ScriptEngine; 
import javax.script.ScriptEngineManager; 
import javax.script.SimpleBindings; 

import javafx.application.Application; 
import javafx.stage.Stage; 

public class GettingStarted extends Application { 

    public static void main(String args[]) { 

     launch(args); 
    } 

    @Override 
    public void start(Stage primaryStage) throws Exception { 
     ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); 

     try (InputStream is = GettingStarted.class.getResourceAsStream("getting-started.js")) { 

      BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
      SimpleBindings bindings = new SimpleBindings(); 
      bindings.put("$STAGE", primaryStage); 
      engine.eval(reader, bindings); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

} 
0

jdk.nashorn.api.scripting.NashornScriptEngineFactory實例化您的Nashorn引擎。它具有getScriptEngine的重載,它們接受一組字符串 - 這些是命令行參數。

不幸的是,你不能通過javax.script接口來做到這一點,但jdk.nashorn.api.scripting也是一個公共和支持的API。

+0

我嘗試使用'NashornScriptEngineFactory'並傳遞'-fx'參數,但它沒有奏效。你的指針雖然有幫助,但是信息豐富,謝謝! – Shiva

0

正如Attila提到的,沒有辦法通過javax.script傳遞參數。你已經兩個選項:

(1)使用犀牛特定的API - >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」工具的。不知道這是否將在腳本引擎 嵌入模式下工作。

+0

感謝您的回答,我很快嘗試了它,但'-fx' arg在這裏不起作用。 – Shiva