简体   繁体   中英

Problem with Runnable jar file in Eclipse: Unable to construct Application instance

I am getting errors when trying to generate a runnable jar for my java project from eclipse. After generating the file, I run

java -jar RunnableAct.jar

which outputs me a lot of errors like:

Exception in Application constructor
Exception in thread "main" java.lang.RuntimeException: Unable to construct Application instance: class application.Main$Main2
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:907)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$160(LauncherImpl.java:819)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at application.AppController.RedirectTo(AppController.java:142)
    at application.AppController.<init>(AppController.java:131)
    at application.Main$Main2.<init>(Main.java:46)
    ... 10 more

My main class looks like:

public class Main {

    public static void main(String[] args) {
        Application.launch(Main2.class, args);
    }

    public static class Main2 extends Application {

        AppController controller = new AppController();

        @Override
        public void start(Stage stage) throws Exception{

            // redirection to main controller class's main method where intro page is called
            Scene scene = new Scene(controller);
            stage.setScene(scene);
            stage.setOnCloseRequest(close -> System.exit(0));
            stage.show();

        }
    }
}

I created the above main method after finding some suggestions around here, otherwise, the first version was:

public class Main extends Application {


    public AppController controller = new AppController();


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


    @Override
    public void start(Stage stage) throws Exception{

        // redirection to main controller class's main method where intro page is called
        Scene scene = new Scene(controller);
        stage.setScene(scene);
        stage.setOnCloseRequest(close -> System.exit(0));
        stage.show();

    }
}

When the AppController class is called, it calls the following method:

public AppController() {    
        RedirectTo("/fxml/Intro.fxml");
    }

    public void RedirectTo(String url) {        
        FXMLLoader loader = new FXMLLoader(getClass().getResource(url));        
        loader.setController(this);
        loader.setRoot(this);
        try {
            loader.load();
        } catch (IOException ex) {
        }   

    }

both print:

java.lang.RuntimeException: Unable to construct Application instance class application.Main$Main2

or with the other main method:

java.lang.RuntimeException: Unable to construct Application instance class application.Main

I would really appreacite your help, thanks!

The "Caused By" clause in the stack trace:

Caused by: java.lang.IllegalStateException: Location is not set.

indicates that the FXMLLoader is unable to find the FXML file.

(Essentially, what happens here is that if the resource is not in the correct place, getClass().getResource(url) will return null. The URL argument you provide to the FXMLLoader constructor is set as the location ; when you try to load the FXML, if the location is null, it throws the exception, indicating that the location has not been set.)

If the code works correctly in the IDE, but issues this exception when you run from a jar file, then there are two common possible causes:

  1. (Not applicable here, but including it for other users.) You are using a resource specification which works in a regular filesystem, but doesn't work in a jar file. Resource specifications for a jar file must strictly follow the Java resource naming specification; in particular, . and .. are not allowed in resource names. In the case in the current question, "/fxml/Intro.fxml" is a valid resource name.
  2. The FXML file has not been deployed (or has been deployed to the wrong path) in the jar file).

A good way to troubleshoot this is to list the contents of the jar file. You can do this from the command line with jar -tf path/to/jar/file.jar (replacing the path with the actual location of your jar file). In your case, /fxml/Intro.fxml is looking for a jar entry called Intro.fxml in the "folder" fxml , which should be in the root of the jar file.

If the entry is not there, you need to ensure the build is configured correctly. This is dependent on your build environment, and most people these days will recommend a build tool such as Maven or Gradle. For a vanilla Eclipse build, you should ensure the folder containing the FXML file is configured as a source folder: you can do this by right-clicking on the project, choosing "Properties", selecting the "Java Build Path" and selecting "Source" in the tabs in the dialog.

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