Invoke java using JavaScript inside JavaFX. Is it possible?


I am using WebEngine & WebView from JavaFX. Now I want to execute Java using javascript running inside WebEngine.

My question is if it is possible to do so and if yes any hints.

I want do something like below

<script type="text/javascript">
  function runSampleJava() {
    var number = new java.lang.Integer(1234);
    var random = new java.util.Random();

Now if I call the runSampleJava() inside WebEngine it will execute that code.

Points to be noted

  • This is not about Rhino JavaScript engine for java
  • I know it is possible to inject java object, eg: JSObject window = (JSObject) webEngine.executeScript("window"); and so on. But this is not what I am looking for.
1/14/2013 12:36:01 PM

I didn't manage to create Java instances but the thing I managed to do is to push object instances created in Java into JavaScript and call back to them.

So my Java-Code looks like this:

JSObject win = (JSObject) engine.executeScript("window");
win.setMember("jHelper", new JavaHelper());

JavaHelper example (must be public):

public static class JavaHelper {
    public int newInteger(int input) {
        // ...
    public Random newRandom() {
        // ...

And then then in JavaScript:

function bla() {
  var number = jHelper.newInteger(1234);
  var random = jHelper.newRandom();
  // ...

You can see my work where I communicate between Java and JavaScript back and forth at and in action at

11/30/2016 7:41:49 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow