3

Project Dirs Structure

I have a Maven project and I am trying to work with javafx (I´m using Eclipse) I´m want to change path of my files. I have

Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml"));

This works fine with the next folder structure

src
  main
    java
      com
        projectFolder
           main.java
      application
        Main.fxml

But I want something like

src
  main
    java
      com
        projectFolder
           main.java
        application
           Main.fxml

I have tried Parent root = FXMLLoader.load(getClass().getResource("/com/application/Main.fxml")); but does not work.

I get the next error

   [JavaFX Application Thread] ERROR com.application.Main - javafx.fxml.LoadException: 
/C:/Users/user/workspace/git/project/target/classes/com/application/Main.fxml:15

Anyone knows what´s wrong with it?

Yahya
  • 13,349
  • 6
  • 30
  • 42
andreas Viena
  • 153
  • 1
  • 1
  • 10

2 Answers2

0

I don't recommend that confusing file structure...

But in this case, I believe you would need to use a relative path to the resource: ../application/Main.fxml

getClass().getResource() expects the resource to be in the same directory as the calling class. It is not, so you would need Java to back up one directory with ..

Zephyr
  • 9,885
  • 4
  • 28
  • 63
  • 1
    Hmm this may actually solve the problem but I am pretty sure thats not going to work when you export your project into an Executable JAR file. At least i had a lot of issues using relative paths in past. – JKostikiadis Jun 18 '18 at 17:20
  • 1
    what structure do you recommend? If I do somethin like FXMLLoader.load(getClass().getResource("../application/Main.fxml")); I get other kind of error: java.lang.NullPointerException: Location is required. – andreas Viena Jun 18 '18 at 17:33
0

Assuming you and I have indeed the same project structure like the picture below :

enter image description here

In order to load the FXML file you should use a full path like the example below :

package main.java.com.projectFolder;

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

public class Main extends Application {

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

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

        FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/main/java/com/application/Main.fxml"));

        Parent root = loader.load();
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }
}

Now in case you actually use a relative path like :

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("../application/Main.fxml"));

The above will also work as long as you are compiling and running the code through the Eclipse runtime environment. The moment you export the project to a JAR file and try to execute it, you will get the same error you are getting from the start. You can actually read about that in similar post like this one : It runs OK in Eclipse but not able to run as .jar file

Edit : For maven project you should consider relocating the FXML file to your src/main/resources/ folder. You could find one more in this post : JavaFX and maven: NullPointerException: Location is required

JKostikiadis
  • 2,847
  • 2
  • 22
  • 34
  • Thanks, but I have it exactly like this FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/main/java/com/application/Main.fxml")); and still get this error [JavaFX Application Thread] ERROR com.application.Main - java.lang.NullPointerException: Location is required. – andreas Viena Jun 18 '18 at 18:01
  • @andreasViena Interesting.. In that case there are two possibilities, either this is a Maven/Gradle Java project, which handles the paths differently and has a different project structure or you don't have the same project structure that I have, showing us a picture of your Navigator View on eclipse could be helpful – JKostikiadis Jun 18 '18 at 18:05
  • yes, I have the same structure. Im going check the maven thing – andreas Viena Jun 18 '18 at 18:11
  • @AndreasViena When you create the project you select the option "Java Project" right? Which IDE are you using ( Eclipse, InteliJ etc)? What Java Version are you running? Maybe the problem lies somewhere else. – JKostikiadis Jun 18 '18 at 18:14
  • It´s a maven proyect. I´m using Eclipse, java 8 – andreas Viena Jun 18 '18 at 18:21
  • @andreasViena Well I am not familiar with Maven, but from what I can tell you need to make some configurations or to use specific folder structure to load resources. Take a look to similar post like this one : https://stackoverflow.com/questions/20348657/cannot-load-properties-file-from-resources-directory – JKostikiadis Jun 18 '18 at 18:23
  • Interesting. The strange thing, is that if I name my package application instade of com.application and do this Parent root = FXMLLoader.load(getClass().getResource("/application/Main.fxml")); it works fine – andreas Viena Jun 18 '18 at 18:45
  • @andreasViena Maybe has something to do about how maven manipulate the files upon compilation. Consider checking the edit on my post to see if you find out about your problem. Also consider adding the information you provide in the commends above to the main post so everyone can see them ( I mean about Maven etc ) also you could add the appropriate maven tag as well. – JKostikiadis Jun 18 '18 at 19:08