Hi guys,

I've made an application in JavaFX and I do want to export it into a runnable jar.
When I run it from Eclipse or Netbeans, it works well.
But when I run it from windows as an executable jar I get this error for my layout.

Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:497)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
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$152(LauncherImpl.java:182)
        at com.sun.javafx.application.LauncherImpl$$Lambda$2/1338668845.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
        at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2438)
        at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2413)
        at application.MainApp.initRootLayout(MainApp.java:47)
        at application.MainApp.start(MainApp.java:38)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
        at com.sun.javafx.application.LauncherImpl$$Lambda$52/2117128939.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
        at com.sun.javafx.application.PlatformImpl$$Lambda$48/1721003345.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
        at com.sun.javafx.application.PlatformImpl$$Lambda$50/268638582.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
        at com.sun.javafx.application.PlatformImpl$$Lambda$49/42105627.run(Unknown Source)
        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$145(WinApplication.java:101)
        at com.sun.glass.ui.win.WinApplication$$Lambda$38/622073264.run(Unknown Source)
        ... 1 more

My code looks like this on the 47'th line loader.setLocation(getClass().getResource("../application/view/RootLayout.fxml"));

private void initRootLayout() {
    try {
        // Load root layout from fxml file.
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("../application/view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();
        // Show the scene containing the root layout
        Scene scene = new Scene(rootLayout);
        //scene.getStylesheets().add("application/application.css");
        primaryStage.setScene(scene);
        primaryStage.show();

    } catch(IOException e) {
        e.printStackTrace();
    }
}

Dani AI

Generated

"Location is not set" means FXMLLoader was given a null URL for your FXML. It can work in the IDE (files on disk) yet fail in the JAR if the resource path is resolved relatively. Using .. in getResource(...) is brittle here. JamesCherrill is on the right track: first confirm the FXML is really inside the JAR under the expected path, then load it via an absolute classpath location.

Try loading with an absolute path (leading slash) and fail fast if it is missing:

URL fxml = getClass().getResource("/application/view/RootLayout.fxml");
if (fxml == null) {
    throw new IllegalStateException("RootLayout.fxml not found on classpath");
}
FXMLLoader loader = new FXMLLoader(fxml);
BorderPane rootLayout = loader.load();

On Windows, inspect the JAR to verify the entry exists exactly at application/view/RootLayout.fxml (case-sensitive inside JARs):

jar tf YourApp.jar | find "application/view/RootLayout.fxml"

If you do not see that path:

  • In NetBeans: place RootLayout.fxml under your source tree using the same package-style folders (eg, src/application/view/RootLayout.fxml). NetBeans will copy it into build/classes and into the JAR automatically.
  • In Eclipse: ensure the folder containing the FXML is on the build path (Right-click folder -> Build Path -> Use as Source Folder), or place it under src/... so it is compiled/copied into the JAR.

A couple of extra gotchas:

  • Do not prefix with ../ when using Class.getResource(...); use an absolute path from the classpath root instead.
  • If you later modularize (Java 9+), this specific error still indicates a missing resource, not module access; fix the classpath path first before looking at modules.

Recommended Answers

All 2 Replies

Are you sure the .fxml file is in the correct directory structure inside your jar?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.