1

I'm trying to export my java and javafx code as a ruunable file, but when executing the jar I get a message that says java.lang.NullPointerException: Location is required. '(You can see in the link that leaves the complete error below), the problem is that if I run the code in the same eclipse if it opens the created program without any problem, but when I export it, the jar does not open. I leave the main.java class that connects with the javafx.

On the other hand, I saw that a solution was to add getClass().getClassLoader().getResource("main.fxml") but that way I don't run the program in eclipse, so I have to leave it as getClass().getResource(" Main.fxml ")) to run in the editor.

Now, to export as a jar a java file with javafx is it exported normal or do other steps have to be followed?

public class Main extends Application {
        
        private double xOffset = 0;
        private double yOffset = 0;
        
        @Override
        public void start(final Stage stage) throws Exception {

                AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Main.fxml"));
                stage.initStyle(StageStyle.TRANSPARENT);
                Scene scene = new Scene(root);
                scene.setFill(javafx.scene.paint.Color.TRANSPARENT);
                scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                stage.setScene(scene);
                stage.show();
                
                
                root.setOnMousePressed(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        xOffset = event.getSceneX();
                        yOffset = event.getSceneY();
                    }
                });
                
                //Permite el movimiento
                root.setOnMouseDragged(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        stage.setX(event.getScreenX() - xOffset);
                        stage.setY(event.getScreenY() - yOffset);
                    }
                });
                
                
        }
        
        public static void main(String[] args) {
            launch(args);
        }
    }

image errorImage

FOLDER Structure folder structure

In jar enter image description here

In IDE ECLIPSE: enter image description here

Talcazul
  • 19
  • 4
  • 1
    Look like the file is not there. Check if below Post helps - https://stackoverflow.com/questions/28266492/location-is-required-exception-when-loading-fxml-file – Krishnom Jun 17 '21 at 01:09
  • I just added a link that shows the image of how the folders are structured and thus arrive at the file. – Talcazul Jun 17 '21 at 01:16
  • How are you running the code? From IDE or using jar? – Krishnom Jun 17 '21 at 01:22
  • from IDE eclipse – Talcazul Jun 17 '21 at 01:31
  • If I add a syso in the column where the fxml file is called, in eclipse the path appears but when exporting it in jar it doesn't. – Talcazul Jun 17 '21 at 02:06
  • So the program errors when you run it using Jar. Can you check the content of jar if file is there? https://stackoverflow.com/questions/47100605/how-to-include-resource-folder-in-executable-jar-file-in-eclipse/47100985 should help – Krishnom Jun 17 '21 at 02:17
  • thanks my friend, I was able to solve the problem, I had to create a folder called resource and there add file. – Talcazul Jun 17 '21 at 02:35
  • no screenshots of text – kleopatra Jun 17 '21 at 02:49

0 Answers0