我不斷收到“此版本中使用了已棄用的 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"]
}
}
}
uj5u.com熱心網友回復:
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”。告訴它任務是針對哪個專案以及要執行哪個任務。
應用程式插件
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/456233.html
