Question

I have a Java application that requires internet access since it embeds a web browser by means of a WebView JavaFX component.

If the application is not packaged in a Jar, it executes without problems. However, when packaged in a Jar it cannot access internet anymore (e.g., it cannot load the remote Javascript files it requires, such as JQuery).

I tried to fix it signing the jar with:

jarsigner myjar myalias

And the command succeeds, although with the following warning:

Warning: The signer certificate will expire within six months. No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2014-07-08) or after any future revocation date.

However, the application still does not have internet access (the embedded browser still cannot load remote script files). I am supposed to sign it in other way ? maybe including a certificate from a recognized certificate authority ?

I have also configured permissions in my ~/.java.policy file as follows:

keystore "file:<userpath>/.keystore", "jks";

grant signedBy "myalias" {
  permission java.security.AllPermission;
};

I am trying in OSX 10.9.2 and Java 1.8.0-b132

UPDATE

It turned out that the embedded browser is executed differently if the application is embedded in a Jar or not, and this goes beyond security permissions.

I do not have idea what is the reason of this, but this can lead you to all sort of wrong conclusions if you do not know it. A nasty bug probably.

For example, one of the things that I observed, is that when no packaged in a Jar, beginning the embedded web page like this worked fine:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
...

But when packaged in a Jar this gave me problems (probably the xhtml1-strict was enforced) so I had to replace it by a simple <html> tag.

This was not the only different behaviour, but the main thing that was giving me problems.

Was it helpful?

Solution

More information would be great, are you trying to deploy with native packaging, are you using webstart, etc..

Anyhow, based on your question, I'd suggest you take a look at letting Netbeans help you with the signing etc.

I prefer creating an FXML based application, that way the code & interface stays separate. In this case I'm just throwing in some HTML directly.

I've deployed a number of JavaFX applications using WebView with no problem. If you're deploying locally, I wouldn't worry about the certificate expiring at this point, unless you're using webstart.

Here's some sample code that works fine, it uses a WebView & accesses the internet to get jQuery.

The main class:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

/**
 *
 * @author Sam
 */
public class WebViewTestJDK8 extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

The controller:

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;


public class FXMLDocumentController implements Initializable {

    @FXML
    private WebView webView;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                String html = "<html>\n"
                        + "<head>\n"
                        + "<title>jQuery Hello World</title>\n"
                        + " \n"
                        + "<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.2.6.min.js\"></script>\n"
                        + " \n"
                        + "</head>\n"
                        + " \n"
                        + "<body>\n"
                        + " \n"
                        + "<script type=\"text/javascript\">\n"
                        + " \n"
                        + "$(document).ready(function(){\n"
                        + " $(\"#msgid\").html(\"Hello World by JQuery\");\n"
                        + "});\n"
                        + " \n"
                        + "</script>\n"
                        + " \n"
                        + "Hello World by HTML\n"
                        + " \n"
                        + "<div id=\"msgid\">\n"
                        + "</div>\n"
                        + " \n"
                        + "</body>\n"
                        + "</html>";

                webView.getEngine().loadContent(html);
            }
        });

    }

}

The FXML:

<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.web.*?>

<AnchorPane id="AnchorPane" fx:id="pane" prefHeight="367.0" prefWidth="446.0" xmlns:fx="http://javafx.com/fxml" fx:controller="webviewtest.jdk8.FXMLDocumentController">
  <children>
    <WebView fx:id="webView" prefHeight="367.0" prefWidth="446.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
  </children>
</AnchorPane>

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top