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

FXML文件无法加载求助:提示文件找不到或返回null

JavaFX FXML加载报错:Location is not set 解决方法

问题场景

尝试通过FXML文件加载JavaFX GUI,但多次修复后仍提示文件找不到或返回null。FXML文件放置在Graphics包中,项目内路径为src/main/java/com/example/javapoker/Graphics/GUI.fxml。

代码实现

package com.example.javapoker.Graphics;

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

import java.io.IOException;
import java.net.URL;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) throws IOException {
        // Load the FXML file using getResource() from the class loader
        URL url = getClass().getResource("/com/example/javapoker/Graphics/GUI.fxml");
        String classpath = System.getProperty("java.class.path");
        System.out.println("Classpath: " + classpath);

        if (url != null) {
            FXMLLoader loader = new FXMLLoader(url);
            primaryStage.setScene(new Scene(loader.load(), 400, 400));
            primaryStage.setTitle("Hello JavaFX");
            primaryStage.show();
        } else {
            System.err.println("Resource 'GUI.fxml' not found");
        }
    }

}

报错堆栈

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:119)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1081)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:893)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:1623)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2556)
    at javafx.fxml@20.0.1/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2531)
    at com.example.javapoker/com.example.javapoker.Graphics.Main.start(Main.java:25)
    at javafx.graphics@20.0.1/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:839)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:483)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:456)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
    at javafx.graphics@20.0.1/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:455)
    at javafx.graphics@20.0.1/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at javafx.graphics@20.0.1/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics@20.0.1/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:185)
    ... 1 more
Exception running application com.example.javapoker.Graphics.Main

注:为获取完整报错堆栈,删除了代码中的空值判断条件

解决方案

  • 调整FXML文件存放目录
    Maven/Gradle等构建工具默认不会将src/main/java下的非.java文件复制到编译输出目录。需将GUI.fxml移至src/main/resources/com/example/javapoker/Graphics/目录,确保编译后资源能被正确打包到classpath中。

  • 修正getResource调用路径
    若FXML文件与Main类同包,可简化路径调用:

    URL url = getClass().getResource("GUI.fxml");
    

    或使用类加载器的绝对路径(需保证resources目录结构正确):

    URL url = getClass().getClassLoader().getResource("com/example/javapoker/Graphics/GUI.fxml");
    
  • 验证编译后资源存在性
    检查编译输出目录(Maven为target/classes,Gradle为build/classes/java/main)下是否存在com/example/javapoker/Graphics/GUI.fxml,若不存在说明资源未被正确复制。

  • Maven额外配置(可选)
    若坚持将FXML放在src/main/java目录,需在pom.xml中添加资源复制规则:

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.fxml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
    </build>
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 22:34:53