Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a simple way to get the JavaScript output in Java (using Eclipse)?

I'm trying to get the return value of a JavaScript function I've written. I can do it using a very complicated method involving listeners, but I'm wanting to use Eclipse's Browser.evaluate method.

As a toy problem, I have the following code in a Java file:

Object result = this.browser.evaluate("new String(\"Hello World\")");

where this.browser is created in a class extending org.eclipse.ui.part.EditorPart in the overridden function createPartControl:

public void createPartControl(Composite parent) {
  // Create the browser instance and have it 
  this.browser = new Browser(parent, SWT.NONE);
  this.browser.addFocusListener(new FocusAdapter() {
    @Override
    public void focusGained(FocusEvent e) {
      SomeEditor.this.getSite().getPage().activate(SomeEditor.this);
    }
  });
  new ErrorReporter(this.browser);
  this.browser.setUrl(Activator.getResourceURL("html/java-shim.html").toString());
  ...more stuff snipped...
}

However, instead of result being a Java String with the contents "Hello World", it's null. Am I using this method incorrectly, or is it broken?

like image 764
Ben Hocking Avatar asked Oct 21 '22 22:10

Ben Hocking


1 Answers

You have to return a value in JavaScript:

Object result = browser.evaluate("return 'Hello World'");

works for me.

like image 117
Udo Klimaschewski Avatar answered Oct 24 '22 15:10

Udo Klimaschewski