JavaFX 和 Gradle:加载默认视图的正确方法是什么?

发布于 2025-01-18 12:08:04 字数 4902 浏览 0 评论 0原文

我不断收到“此版本中使用了已弃用的 Gradle 功能,使其与 Gradle 8.0 不兼容。”

我已经查看并尝试了十几个修复和教程,但都出现相同的错误。我已将视图移至每个文件夹并重命名,以防不允许使用下划线。我已经尝试了“/path/file.fxml”的每一种排列,我能想到

我有一种感觉我的代码被贬值了,但我所能做的就是让 intellij 突出显示 .load()

这是我的代码:

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

public class ScoreSheet extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader fxmlLoader = new 
FXMLLoader(ScoreSheet.class.getResource("views/main_menu.fxml"));
    primaryStage.setTitle("Score Sheet");
    Group root = new Group();
    Scene scene = new Scene(fxmlLoader.load(), 405, 720);
    primaryStage.setScene(scene);
    primaryStage.show();

    }
}

这是我的堆栈跟踪:

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:51285', transport: 'socket'
Gradle Daemon started in 616 ms

> Configure project :
Found module name 'ScoreSheetTest.main'
Disconnected from the target VM, address: '127.0.0.1:51285', transport: 'socket'

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
Connected to the target VM, address: 'localhost:51290', transport: 'socket'
Disconnected from the target VM, address: 'localhost:51290', transport: 'socket'
Connected to the target VM, address: '127.0.0.1:51285', transport: 'socket'

> Task :ScoreSheet.main() FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings
3 actionable tasks: 1 executed, 2 up-to-date
Exception in Application start method
Exception in thread "main" 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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/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/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at ScoreSheetTest.main/com.company.scoresheet.ScoreSheet.start(ScoreSheet.java:16)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ScoreSheet.main()'.
> Process 'command '/Users/username/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1


BUILD FAILED in 4s
Disconnected from the target VM, address: '127.0.0.1:51285', transport: 'socket'
2:05:57 PM: Execution finished ':ScoreSheet.main()'.

编辑:文件结构图像 文件结构图像

编辑2:这是我的build.gradle:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

group 'com.iharptech'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'org.openjfx:javafx-controls:18'
}

test {
    useJUnitPlatform()
}

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/java"]
            includes = ["**/*.fxml"]
        }
    }
}

I keep getting "Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0."

I've looked and tried a dozen fixes and tutorials all with the same error. I've moved the view to the every folder and renamed it incase underscores aren't allowed. I've tried every permutation of "/path/file.fxml" I can think of

I have a feeling my code is depreciated but all I can do is get intellij to highlight .load()

Here is my code:

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

public class ScoreSheet extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader fxmlLoader = new 
FXMLLoader(ScoreSheet.class.getResource("views/main_menu.fxml"));
    primaryStage.setTitle("Score Sheet");
    Group root = new Group();
    Scene scene = new Scene(fxmlLoader.load(), 405, 720);
    primaryStage.setScene(scene);
    primaryStage.show();

    }
}

Here is my stacktrace:

Starting Gradle Daemon...
Connected to the target VM, address: '127.0.0.1:51285', transport: 'socket'
Gradle Daemon started in 616 ms

> Configure project :
Found module name 'ScoreSheetTest.main'
Disconnected from the target VM, address: '127.0.0.1:51285', transport: 'socket'

> Task :compileJava UP-TO-DATE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar UP-TO-DATE
Connected to the target VM, address: 'localhost:51290', transport: 'socket'
Disconnected from the target VM, address: 'localhost:51290', transport: 'socket'
Connected to the target VM, address: '127.0.0.1:51285', transport: 'socket'

> Task :ScoreSheet.main() FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.2/userguide/command_line_interface.html#sec:command_line_warnings
3 actionable tasks: 1 executed, 2 up-to-date
Exception in Application start method
Exception in thread "main" 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 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/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/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2541)
    at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2516)
    at ScoreSheetTest.main/com.company.scoresheet.ScoreSheet.start(ScoreSheet.java:16)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ScoreSheet.main()'.
> Process 'command '/Users/username/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1


BUILD FAILED in 4s
Disconnected from the target VM, address: '127.0.0.1:51285', transport: 'socket'
2:05:57 PM: Execution finished ':ScoreSheet.main()'.

Edit: File structure image File Structure Image

Edit 2: Here is my build.gradle:

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.8'
}

group 'com.iharptech'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
    implementation 'org.openjfx:javafx-controls:18'
}

test {
    useJUnitPlatform()
}

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/java"]
            includes = ["**/*.fxml"]
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放低过去 2025-01-25 12:08:04

Gradle 弃用警告

这些警告几乎肯定与您的 IllegalStateException 无关。

如果您想知道使用了哪些已弃用的功能,则可以按照警告消息所述操作并使用 --warning-mode all。如果您自己的构建脚本使用了这些功能,那么您可以查找替换项并自行修复问题。但是,如果插件使用了这些功能,那么您唯一的选择是:(1)更新以使用插件的最新版本; (2) 如果已经使用最新版本,请向插件作者提交一份错误报告(如果尚不存在); (3) 如果可能的话,也许使用不同的插件。


在 IDE 中使用 Gradle

当您使用 Gradle(或 Maven)等构建工具时,您应该让构建工具处理所有依赖项和其他配置(尽可能多)。换句话说,您应该在构建脚本中声明 JavaFX 依赖项,而不是在 IntelliJ 中。

您还应该让 IDE 将构建和运行任务委托给 Gradle。这样,Gradle 负责所有与构建相关的事情,而 IDE 只负责让代码编写变得更容易。

IntelliJ 与 Gradle 配合得非常好。 IntelliJ 会识别您添加到 Gradle 的任何依赖项。 Gradle 提供的任何任务也将被 IntelliJ 识别。

在 Gradle 中声明 JavaFX 依赖项

您正在使用 OpenJFX javafx-gradle-plugin。该项目的自述文件包含有关如何声明项目所需的 JavaFX 模块的文档。 请注意,该插件的最新版本是0.0.12。您应该更新构建脚本以使用该版本。

本地 JavaFX SDK

如果您使用的是下载的 JavaFX SDK,正如您的评论似乎暗示的那样,那么您可以使用:

javafx {
    sdk = '/path/to/sdk' // replace with your own path
    modules = ['javafx.controls', 'javafx.fxml'] // modify list as needed
}

对于 sdk 路径,您可能需要设置一个变量,然后由调用 Gradle 的人。这样,其他计算机上的人们仍然可以构建您的项目,而无需修改构建脚本。不过,如果这是一台计算机上的个人项目,那么静态的绝对路径目前应该没问题。

我不知道此功能何时添加到插件中,因此您可能需要更新到最新版本。

Maven Central JavaFX JAR

您可以让 Gradle 从 Maven Central 下载所需的 JavaFX JAR 文件。

javafx {
    modules = ['javafx.controls', 'javafx.fxml'] // modify list as needed
    version = '18' // change version as needed
}

repositories {
    mavenCentral()
}

执行 Gradle 任务

您应该进行运行配置来执行 Gradle 任务,而不是让 IDE 使用自己的构建和执行系统。

要通过 IntelliJ 执行任何 Gradle 任务,您可以打开 Gradle 选项卡(如果我没记错的话,通常位于 IntelliJ 的右侧),选择一个任务并执行它。

或者您可以手动创建“运行配置”。选择配置类型时请务必选择“Gradle”。告诉它该任务是针对哪个项目以及要执行哪个任务。

应用程序插件

通常,对于 JVM 项目,当您希望能够执行项目时,您应该应用 application 插件。

plugins {
    // other plugins...
    'application'
}

你已经这样做了。但您还需要配置主类和主模块(如果存在)。

application {
    mainModule = '<module-name>'
    mainClass = '<fully-qualified-class-name>'
}

application 插件添加了 run 任务。要执行您的项目,请让 IntelliJ 执行 run 任务。


资源

有关资源、如何加载资源、使用哪些路径以及一些故障排除技术的一般信息,我推荐此问答:

如何我如何确定 JavaFX 应用程序所需的 FXML 文件、CSS 文件、图像和其他资源的正确路径?

但具体到您的设置,您应该从 Gradle 构建脚本中删除这部分:

sourceSets {
    主要的 {
        资源 {
            srcDirs = ["src/main/java"]
            包括= [“**/*.fxml”]
        }
    }
}

不需要。保留它意味着 src/main/resources 不再被视为资源根,这可能是您不想要的。

进行此更改后,并通过 run 任务执行项目,使用 "/views/main_menu.fxml" 作为资源路径应该适合您。

Gradle Deprecation Warnings

These warnings are almost certainly not related to your IllegalStateException.

If you want to know which deprecated features were used, then you can do what the warning message says and use --warning-mode all. If the features were used by your own build script, then you can look up the replacements and fix the problem yourself. However, if the features were used by a plugin then really your only options are: (1) Update to use the latest version of the plugin; (2) If already using the latest version, submit a bug report with the plugin authors (if one doesn't already exist); (3) Maybe, if possible, use a different plugin.


Using Gradle with an IDE

When you use a build tool such as Gradle (or e.g., Maven), then you should let the build tool handle all dependencies and other configurations (as much as is possible). In other words, you should be declaring the JavaFX dependencies in your build script, not in IntelliJ.

You should also have the IDE delegate build and run tasks to Gradle. That way Gradle is responsible for everything build related, and the IDE is only responsible for making it easier to write code.

IntelliJ works really well with Gradle. Any dependencies you add to Gradle will be known by IntelliJ. And any tasks supplied by Gradle will also be known by IntelliJ.

Declare JavaFX Dependencies in Gradle

You're using the OpenJFX javafx-gradle-plugin. That project's README has documentation for how to declare which JavaFX modules you need for your project. Note the latest version of this plugin is 0.0.12. You should update your build script to use that version.

Local JavaFX SDK

If you're using a downloaded JavaFX SDK, as your comments seem to imply, then you can use:

javafx {
    sdk = '/path/to/sdk' // replace with your own path
    modules = ['javafx.controls', 'javafx.fxml'] // modify list as needed
}

For the sdk path, you may want to setup a variable that is then passed by the person who invokes Gradle. That way people on other computers can still build your project without modifying the build script. Though if this is a personal project on a single computer then a static, absolute path should be fine for now.

I don't know when this feature was added to the plugin, so you may need to update to the latest version.

Maven Central JavaFX JARs

You could instead have Gradle download the needed JavaFX JAR files from Maven Central.

javafx {
    modules = ['javafx.controls', 'javafx.fxml'] // modify list as needed
    version = '18' // change version as needed
}

repositories {
    mavenCentral()
}

Executing Gradle Tasks

You should make run configurations to execute Gradle tasks, rather than letting the IDE use its own build and execution system.

To execute any Gradle task via IntelliJ, you can open up the Gradle tab (typically on the right side of IntelliJ, if I'm not mistaken), select a task, and execute it.

Or you could manually create a "run configuration". Make sure to choose "Gradle" when selecting the type of configuration. Tell it which project the task is for and which task to execute.

The Application Plugin

Typically, with JVM projects, when you want to be able to execute your project you should apply the application plugin.

plugins {
    // other plugins...
    'application'
}

You already do this. But you also need to configure the main class and, if present, the main module.

application {
    mainModule = '<module-name>'
    mainClass = '<fully-qualified-class-name>'
}

The application plugin adds the run task. To execute your project, have IntelliJ execute the run task.


Resources

For general information about resources, how to load them, what paths to use, and some troubleshooting techniques, I recommend this Q&A:

How do I determine the correct path for FXML files, CSS files, Images, and other resources needed by my JavaFX Application?

But specifically regarding your setup, you should remove this part from your Gradle build script:

sourceSets {
    main {
        resources {
            srcDirs = ["src/main/java"]
            includes = ["**/*.fxml"]
        }
    }
}

It's not needed. Keeping it means src/main/resources is no longer considered a resource root, which you probably do not want.

With that change, and executing the project via the run task, using "/views/main_menu.fxml" should work for you as the resource path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文