简体   繁体   中英

How to get javafx WebView to load resources from a jar file

I'm trying to get a javafx.scene.web.WebView to load resources from a jar file, but it doesn't want to do it, and doesn't produce any meaningful error.

The jar file is an OSGi bundle, specifically a cytoscape app (plugin) bundle.

Here's my code: (it all works normally if i put https://google.com as the url).

webView.getEngine().getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
    @Override
    public void changed(ObservableValue<? extends Throwable> ov, Throwable t, Throwable t1) {
        System.out.println("Received exception: "+t1.getMessage() + " " + t1.getClass().getName());
        t1.printStackTrace();
    }
});

webView.getEngine().getLoadWorker().messageProperty().addListener(new ChangeListener<String>() {
    @Override
    public void changed(ObservableValue<? extends String> ov, String t, String t1) {
        System.out.println("message:" + t1);
    }
});

String url = getClass().getResource("/foo.html").toExternalForm();
webView.getEngine().load(url);
System.out.println("contents:" + new String(getClass().getResourceAsStream("/foo.html").readAllBytes()));   

That prints:

message:Loading bundle://202.0:1/foo.html                                                                                                                                        
contents:<p>hello, webview</p>
message:Loading failed
Received exception: Unknown error java.lang.Throwable
    at javafx.web/javafx.scene.web.WebEngine$LoadWorker.describeError(WebEngine.java:1431)
    at javafx.web/javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(WebEngine.java:1370)
    at javafx.web/javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(WebEngine.java:1231)
    at javafx.web/com.sun.webkit.WebPage.fireLoadEvent(WebPage.java:2514)
    at javafx.web/com.sun.webkit.WebPage.fwkFireLoadEvent(WebPage.java:2359)
    at javafx.web/com.sun.webkit.Timer.twkFireTimerEvent(Native Method)
    at javafx.web/com.sun.webkit.Timer.fireTimerEvent(Timer.java:83)
    at javafx.web/com.sun.webkit.Timer.notifyTick(Timer.java:64)
    at javafx.web/javafx.scene.web.WebEngine$PulseTimer.lambda$static$0(WebEngine.java:1192)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

I suspect the problem is WebView doesn't know about these bundle: URLs. But I don't see an api in WebEngine that would allow me to implement a new URL scheme.

@james_d is right -- the problem is the URL you are getting from the getResource is an OSGi bundle: uri, which WebView doesn't know how to read. You can take a look at our CyPlot code ( https://github.com/RBVI/CyPlot ) to see how we've worked around that to read in html and Javascript and hand that off to Cytoscape's web browser.

-- scooter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM