Reputation: 11
I'm developping an desktop app in javafx, and unlike Intellij he jar file return null for getClass().getResource(pagePath)
.
When i build and run the app with Intellij all work fine and do the job, but after i created the artifact and run the .jar file with a command line i got an error :
'Exception in thread "JavaFX Application Thread"
java.lang.NulPointerException: Location is required.' After many researches and tests i found out that the call getClass().getResource(pagepath)
return null in the .jar but not with Intellij. I've tried to change the way to write the path, change the patterns and change the project' structure.
Here the incriminated method
protected void loadPage(ActionEvent event, String pagePath) throws IOException {
System.out.println(getClass().getResource(pagePath));
Parent pageParent = FXMLLoader.load(getClass().getResource(pagePath));
Scene newPage = new Scene(pageParent);
pageParent.getStylesheets().add(Views.CSS);
Stage window = (Stage) ((javafx.scene.Node)event.getSource()).getScene().getWindow();
window.setScene(newPage);
window.show();
}
The initialisation of the programm work, the .jar file launch the first page correctly with this method, in the main class.
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource(Views.XML_ACCUEIL));
primaryStage.getIcons().add(new Image(Views.APP_ICON));
primaryStage.setTitle(Views.APP_NAME);
primaryStage.setScene(new Scene(root, Views.WIDTH, Views.HEIGHT));
primaryStage.show();
}
The View class look like this:
public class Views {
public static final String home = System.getProperty("user.home");
public static final String REGISTER_FOLDER = home + File.separator + "Desktop" + File.separator;
public static final String XML_ACCUEIL = "resources/accueil.fxml";
public static final String XML_VENTE_FORM = "resources/vente_form.fxml";
public static final String XML_DEPOT_FORM = "resources/depot_form.fxml";
public static final String XML_TECHNIQUE_FORM_1 = "resources/technique_form_1.fxml";
public static final String XML_TECHNIQUE_FORM_2 = "resources/technique_form_2.fxml";
public static final String XML_TECHNIQUE_FORM_3 = "resources/technique_form_3.fxml";
//{some others var}
public static final String CSS = "sample/resources/style.css";
public static final String APP_NAME = "World Of Cars Desktop";
public static final String APP_ICON = "sample/resources/logo_petit.png";
public static final int WIDTH = 1280;
public static final int HEIGHT = 720;
}
Here's the structure:
-src
-sample
-controller
{all my controller class}
-resources
{all my fxml files}
-DocFiller
-Main
-Views
With Intellij all work perfectly like i said, but the .jar file just launch the home page and when i use a button, normally the loadPage method is called and the next page appear but instead of that, the error is throw when i try to lad any other page than the home page. All the pages, images and ressources are store and called with the same way.
Upvotes: 0
Views: 1182
Reputation: 2052
SHORT answer: its missign reource folder after compile the project, by default it put all your class and fxml in same folder which cause the issue, you need resource folder seprate same as src strcture.
LONG answer: Manually add resources folder and add src/main/reource from the project as given below imgage, this will copy all resource to requires destination.
at last use below code to fetch correct file to JAVAFX
FXMLLoader.load(SplashController.class.getResource("DashFX.fxml"))
Upvotes: 0
Reputation: 109547
I would have expected "/accueil.fxml"
or "/resources/accueil.fxml"
. Look in the jar with 7tzip or such what the real path is.
The resource path "resources/accueil.fxml"
indicates that in the class path, under the subdirectory of getClass()
(the class' package directory) there should be a subdirectory resources
.
An other error would be that the file names are only case-insensitive in Windows, when not starting a jar. A jar (zip) file, Linux and Mac all have case-sensitive file names. Check the case-sensitive spelling.
Upvotes: 1