JDK-8170085 : Javafx Webengine JSObect members working inconsistently
  • Type: Bug
  • Component: javafx
  • Sub-Component: web
  • Affected Version: 8u111
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2016-11-16
  • Updated: 2016-12-17
  • Resolved: 2016-11-21
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9Resolved
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]

A DESCRIPTION OF THE PROBLEM :
JavaFX code which has worked in previous builds of Java 8 has become unstable and unusable since update 111.

Adding a Java object to javascript does not run on every action. It may work after at least one reload of the page but it is not consistent.

REGRESSION.  Last worked in version 8u101

ADDITIONAL REGRESSION INFORMATION: 
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Create a JavaFX application using WebEngine and set the JSObject and set a Java Object member. A simple one that closes the application is sufficient. In the Javascript on a HTML page try to call the method from the Java Object. Use Java 1.8.0_111 or 112 to compile and run the code. If you change back to Java 1.8.0_101 to compile and run the same code it works.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Nothing happens
ACTUAL -
The application should close

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No Errors

REPRODUCIBILITY :
This bug can be reproduced often.

---------- BEGIN SOURCE ----------
package com.groundsure.javafxtest;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import netscape.javascript.JSObject;


public class MainApp extends Application {   
    private Stage stage;
    
    @Override
    public void start(Stage stage) throws Exception {
        this.stage = stage;
        this.stage.setTitle("Test");
        this.stage.setScene(new Scene(new WebPage(this),1350,750, Color.web("#666970")));
       
        stage.show();
    }
    
    public class WebPage extends Region {
        final WebView browser = new WebView();
        final WebEngine webEngine = browser.getEngine();
        
        public WebPage(final Application main) {
            getChildren().add(browser);
            webEngine.loadContent("</div><br /><div onClick='app.exit();' width='100' height='50'>" +
                                  "Exit</div>");

            webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                @Override
                public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State newState) {
                    if (newState.equals(Worker.State.SUCCEEDED)) {                    
                        JSObject win = (JSObject) webEngine.executeScript("window");
                        win.setMember("app", new App());
                    }
                }
            });
        }
    }
    
    public class App {
        public void exit() {
            Platform.exit();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Not using the latest version of java


Comments
This is a behaviour change from 8u102 as as part of the fix JDK-8089681 (Javascript to java uses weak references) Please refer JDK-8154127, JDK-8169204 for the documentation changes. if you want your application to work , you need to change something like: private App obj = new App(); if (newState.equals(Worker.State.SUCCEEDED)) { JSObject win = (JSObject) webEngine.executeScript("window"); win.setMember("app", obj); }
21-11-2016