0

First of all, my first question here. Found similar problems but either they are not solved or not the same. So here it goes. I have a javafx desktop application. It works just fine on intellij but when i build a .jar file, it doesn't. What i do not understand is why it is trying to find it in C: directory. Shouldn't it be looking for in jar file? How can i fix this?

when i try to run in command prompt it says this:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: C:\Users\MYUSERNAME\src\resources\loginPage.fxml (The 
system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2440)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:24)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
    ... 1 more
Exception running application application.Main

1 Answers1

1

I think you are not copying correctly the resources inside your .jar file or you are calling it wrongly.

This is how you should load it:

    stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("/views/ExampleView.fxml"))));

This is how you should organise your fxml views inside the resource folder: Check this pic

Snix
  • 541
  • 4
  • 12
  • i am thinking the same thing actually right now. Scene scene = new Scene(FXML.load(getClass().getResource("src//resources//loginPage.fxml"))); i added this line but i got an error that says, cannot resolve method load in FXML – Şükrü AYDINLIK Jun 03 '20 at 22:40
  • OK i got it. It is not FXML.load , it is FXMLLoader.load() but that did not help either. On Ide, now it says Caused by: java.lang.NullPointerException: Location is required. – Şükrü AYDINLIK Jun 03 '20 at 22:45
  • You shouldn't include the resource path inside the getResource. You should write it as follow `stage.setScene(new Scene(FXMLLoader.load(getClass().getResource("loginPage.fxml"))));` Sorry, it's FXMLLoader and not FXML, my mistake – Snix Jun 03 '20 at 22:47
  • `Caused by: java.lang.NullPointerException: Location is required` , it happens because you are specifying wrongly the pathname.. if it's at the root you should use only `loginPage.fxml` as location. Just check my last answer for full code snippet – Snix Jun 03 '20 at 22:49
  • Thanks! that worked for me. For the ones who need it, here is the complete start method i will also edit the question: public void start(Stage stage) throws Exception { parentWindow = stage; Scene scene = new Scene(FXMLLoader.load(getClass().getResource("/loginPage.fxml"))); stage.setScene(scene); stage.show(); – Şükrü AYDINLIK Jun 03 '20 at 22:56
  • 1
    @ŞükrüAYDINLIK The `src` folder is not available at runtime - it makes absolutely no sense to have that as part of the path of the FXML. See https://stackoverflow.com/questions/61531317/how-do-i-determine-the-correct-path-for-fxml-files-css-files-images-and-other for a complete discussion. – James_D Jun 03 '20 at 22:58
  • @James_D thanks for your help. i cannot believe i still struggle with these after 3 years – Şükrü AYDINLIK Jun 03 '20 at 23:01