5

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();
    java.lang.System.out.println(random.nextInt());
  }
</script>

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.
2
  • Maybe this helps: nusphere.com/kb/jscoreref/javobj.html Commented Jan 14, 2013 at 15:50
  • @Fildor it is way beyond the problem :-( Commented Jan 15, 2013 at 10:48

1 Answer 1

4

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 https://github.com/tomsontom/fx-ide/tree/master/at.bestsolution.javafx.ide.editor and in action at http://tomsondev.bestsolution.at/2012/10/29/eclipsecon-javafx-demo-app-videos/

Sign up to request clarification or add additional context in comments.

3 Comments

As I have already specified, I can do it already. And this is not exactly what I am looking for. 'Specially' I need to create object.
I guess it would be a major security problem if the webview would be allowed to create arbitary objects and pass back and forth real java objects. So all I found was allowed to get passed between JavaScript and Java in webview are primitive types and strings (I use JSON from communicating with the Java-Side)
Agreed with that security flaw.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.