Search code examples
javajavafxjarnullpointerexceptionjavafx-8

JavaFX jar is not loading with JAR file but loads fine when compiling code as application


I can build and start the program just fine when compiling as an application in IntelliJ. However, nothing happens when I package and deploy and I try to double-click the jar. I decided to use Git Bash and run it from there and I see it is throwing a NullPointerException. Below is the JavaFX main class, my file structure, and the error that is being thrown.

Why am I getting a NullPointerException in the jar but works fine when I build and compile in IntelliJ?

Please let me know if I'm leaving any information out. Thanks!

public class Main extends Application {

    private Stage stage;

    @Override
    public void start(Stage stage){
        this.stage = stage;
        this.stage.setTitle("My JavaFX App");
        initRootlayout();
    }

    private void initRootlayout(){
        try{
            Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
            Scene scene = new Scene(root);
            stage.setScene(scene);
            scene.getStylesheets().add("css/default.css");
            stage.setResizable(false);
            stage.show();
        }

        catch(Exception e){
            throw new RuntimeException(e);
        }
    }

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

File Structure

Error being thrown


Solution

  • check your jar content mostly it is missing your resources folder. if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab -> add copy of -> Directory Content -> Select your resources folder. then rebuild your jar