You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFX加载FXML失败报Location未设置异常求助

问题排查:JavaFX无法加载FXML文件

问题背景

正在将个人项目从Swing GUI迁移至JavaFX,使用SceneBuilder时无法加载FXML文件,报错信息如下:

错误堆栈

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml@18.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at testify.Runtime.App.start(App.java:22)
    at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics@18.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics@18.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@18.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application testify.Runtime.App

Process finished with exit code 1

项目结构

项目结构截图

主代码

package testify.Runtime;

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

import java.io.IOException;

public class App extends Application {

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

 @Override
 public void start(Stage primaryStage) throws IOException {
    FXMLLoader loader= new FXMLLoader(getClass().getResource("main.fxml"));
    Parent root = (Parent) loader.load();
    Scene home = new Scene(root);

     primaryStage.setTitle("Testify");
     primaryStage.setScene(home); 
         primaryStage.show();
    }
}

问题原因及解决方法

报错IllegalStateException: Location is not set是因为FXML文件路径不正确:

  • 当前代码用getClass().getResource("main.fxml")会在testify.Runtime包的目录下查找文件,但main.fxml实际在src/main/resources根目录下。

修复方案(二选一)

  1. 修改资源路径为绝对路径
    在文件名前加斜杠,让加载器从资源根目录查找:

    FXMLLoader loader= new FXMLLoader(getClass().getResource("/main.fxml"));
    
  2. 使用类加载器加载
    直接通过类加载器获取资源,默认从资源根目录开始查找:

    FXMLLoader loader= new FXMLLoader(getClass().getClassLoader().getResource("main.fxml"));
    

额外检查:确保main.fxml在构建时被正确复制到输出目录(如target/classes),多数IDE会自动处理resources目录下的文件,若未自动复制可手动配置构建路径。

内容的提问来源于stack exchange,提问作者Hopelessdecoy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 04:54:24