Search code examples
javascriptjavauser-interfacenashorn

Using Nashorn/JavaScript to Create a Java GUI application


I am attempting to create an application(game) that would use java to operate the physics,logic, etc. but would use existing javascript libraries for doing things like scripting, graphics, and the ui, but when loading a chart.js(and other libraries) through nashorn nashorn.eval("load('chart.js')"); I get the following (or similar for other libraries) Exception in thread "main" javax.script.ScriptException: ReferenceError: "window" is not defined in chart.js at line number 668.

Is it possible to use these libraries as-is, or would any modification by necessary, and how could I display scripts in a java application(or is it even possible, with or without nashorn)?

Thanks.


Solution

  • You may, however, be able to persuade this to run in the JavaFX WebView container. I have not tried using advanced JavaScript libraries in the container but my experience is:

    • The container works fairly well,
    • It is difficult to debug applications running in it, as there are very few mechanisms for doing so,
    • The mechanisms for communicating back to the Java application are fairly good,
    • You can provide custom handling for application-specific protocols for URLs, but it is not clear to me how these would affect the WebView's security model (unless you updated the http: or https: stream handlers, which appears to be possible using the URL.setURLStreamHandlerFactory method).