简体   繁体   中英

Can't run a simple JavaFX in a Browser packaged by Netbeans IDE

I created the standard NetBeans JavaFX application and I tried to run it in a browser but I had no luck. I have followed every instruction out there with no luck. I am not doing anything special, standard JavaFX and built it with NetBeans IDE.

Here is the error message I am getting:

CacheEntry[file:/Users/mo/NetBeansProjects/WebAppTest/dist/WebAppTest.jar]: updateAvailable=false,lastModified=Fri Mar 04 07:03:31 EST 2016,length=19089
    java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
        at java.security.AccessController.checkPermission(AccessController.java:884)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1294)
        at java.lang.System.getProperty(System.java:717)
        at java.io.UnixFileSystem.resolve(UnixFileSystem.java:133)
        at java.io.File.getAbsolutePath(File.java:556)
        at webapptest.WebAppTest.start(WebAppTest.java:24)
        at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
        at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:150)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "user.dir" "read")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
        at java.security.AccessController.checkPermission(AccessController.java:884)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at sun.plugin2.applet.FXAppletSecurityManager.checkPermission(Unknown Source)
        at java.lang.SecurityManager.checkPropertyAccess(SecurityManager.java:1294)
        at java.lang.System.getProperty(System.java:717)
        at java.io.UnixFileSystem.resolve(UnixFileSystem.java:133)
        at java.io.File.getAbsolutePath(File.java:556)
        at webapptest.WebAppTest.start(WebAppTest.java:24)
        at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
        ... 4 more

JavaFX application:

FXML File

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

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

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="webapptest.FXMLDocumentController">
    <children>
        <Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
        <Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
    </children>
</AnchorPane>

Controller:

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
    }

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

}

main method

public class WebAppTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        File f = new File("FXMLDocument.fxml");
        String filePath = f.getAbsolutePath();
        Parent root = FXMLLoader.load(getClass().getResource(filePath));
        System.out.println(filePath);

        Scene scene = new Scene(root);

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

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

}

Due to the Certificate signing issues: I manually trusted the dist\\ directory:

Regarding the FXML File, If I didn't do getAbsolutePath(); and I get the resources using the line code below. I get a different error message:

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

if I didn't use getAbsolutePath() I get the following error:

updateAvailable=true,lastModified=Fri Mar 04 09:12:56 EST 2016,length=19089
java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at webapptest.WebAppTest.start(WebAppTest.java:26)
    at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.NullPointerException: Location is required.
    at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:150)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at webapptest.WebAppTest.start(WebAppTest.java:26)
    at com.sun.javafx.applet.FXApplet2$2.run(FXApplet2.java:134)
    ... 4 more

Here is the content of the Jar file: 

在此处输入图片说明

Since you updated your answer, it's now clear what you actually want to do. Instead of converting a classpath resource (as retrieved with getClass().getResource() ) to a file, you should just use the resource directly:

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

You do not want to deal with the File class at all, since the resource is coming from inside of a JAR file. Ditch all of that code.

Also, make sure you have the FXMLDocument.fxml placed inside of the webapptest package. If you have it placed at the root of the classpath, use .getResource("/FXMLDocument.fxml") instead.

To include what has been mentioned in comments: You might need to clean and rebuild your project in order for the resources to show up properly.

The problem is that you are denied permission to the filesystem.

In order for your applet to receive file system permissions, it must either be signed with a code certificate (which costs money) or you need to add a security exception in the Java applet policy file.

Also see:

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