2

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.

Lawrence D'Anna
  • 2,998
  • 2
  • 22
  • 25
  • Can you show the complete stack trace (`t1.printStackTrace()`)? Perhaps also log that url? – James_D Jun 02 '20 at 20:58
  • That's weird though. Note, since you've demonstrated you can read the content of the URL directly, you might have a workaround via `String content = new String(getClass().getResourceAsStream("/foo.html").readAllBytes());` and then `webView.getEngine().loadContent(content);` (Don't know what might happen to relative urls in the html though...) – James_D Jun 02 '20 at 21:05
  • @james_d, yea but i was hoping to do foo.css next. I guess i could pack everything into a single HTML as a workaround – Lawrence D'Anna Jun 02 '20 at 21:08
  • The javadocs for `WebEngine` state that the URL handling is via the `java.net` package, which does have API for custom content handlers (but it's 15 years since I looked at that, and I don't remember). I know there are Cytoscape plugins that use JavaFX, and if I remember correctly `WebView` was one of the reasons for doing so, so someone may have solved this problem. I'd drop a message to the Cytoscape forum and see if anyone can help. – James_D Jun 02 '20 at 21:11

1 Answers1

0

@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

Scooter Morris
  • 1,269
  • 1
  • 7
  • 4