关于 java:使用 Gradle 在 IntelliJ 中运行 JavaFX 项目

Running JavaFX project in IntelliJ with Gradle

我在 IntelliJ IDE 中使用 JavaFX 和 Gradle 运行 Java 程序时遇到问题。我已经研究了许多解决方案,似乎是同样的问题,但是这些都没有奏效。

这个问题:IntelliJ Gradle JavaFX 构建,但未运行与我遇到的问题以及我尝试过的修复非常相似。但是,该解决方案对我不起作用。

目前我正在尝试运行一个非常简单的应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class GUI extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        var pane = new BorderPane();
        var input = new TextField();
        primaryStage.setScene(new Scene(pane));
        primaryStage.show();
    }
}

此应用程序在使用命令行和 \\'gradlew run\\' 时确实运行,但是在使用 IntelliJ IDEA IDE 时它不运行。

奇怪的是,如果我删除 TextField \\'input\\',程序运行没有问题。这让我相信问题与模块 java.controls 有关。但是这是在我的 build.gradle:

中导入的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
plugins {
    id 'java'

    id 'application'

    id 'jacoco'

    id 'org.openjfx.javafxplugin' version '0.0.8'
}

javafx {
    modules = [ 'javafx.controls', 'javafx.fxml' ]
}

repositories {
    jcenter()
}

dependencies {
    // Use JUnit Jupiter Engine for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.5.1'
    // Allow for JUnit 4 legacy
    testCompileOnly 'junit:junit:4.12'
    testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.5.1'
}

application {
    mainClassName = 'bfst20.addressparser.GUI'
}

test {
    useJUnitPlatform()
}

jar {
    manifest {
        attributes 'Main-Class': application.mainClassName
    }
}

尝试使用 TextField 运行程序时出现这 2 个错误:

Errors

查看项目库时,您会看到以下概述:

Project

1
2
3
4
5
6
7
8
9
> Task :run
Caching disabled for task ':run' because:
  Build cache is disabled
Task ':run' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
Starting process 'command 'C:\\Program Files\\Java\\jdk-11.0.4\\bin\\java.exe''. Working directory: C:\\Users\\ASUS\\FST\\handin1 Command: C:\\Program Files\\Java\\jdk-11.0.4\\bin\\java.exe --add-modules javafx.controls,javafx.fxml --module-path C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-fxml\\12.0.1\\5397068b7fb8f8db6631242d78e14f7d6db07d51\\javafx-fxml-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-controls\\12.0.1\\838555460c025b8df0fbc0fa03bf3f3767698f89\\javafx-controls-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-graphics\\12.0.1\\18a01ffdd4edb82e7da3bc99937c6a608d1eaaa6\\javafx-graphics-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-base\\12.0.1\\6f5947b255bb26e072862b3bb0c32d54d8effe84\\javafx-base-12.0.1-win.jar -Dfile.encoding=windows-1252 -Duser.country=DK -Duser.language=da -Duser.variant -cp C:\\Users\\sande\\OneDrive\\Dokumenter\\ITU\\2. Semester\\FST\\handin1\\build\\classes\\java\\main;C:\\Users\\sande\\OneDrive\\Dokumenter\\ITU\\2. Semester\\FST\\handin1\\build\
esources\\main;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-fxml\\12.0.1\\5397068b7fb8f8db6631242d78e14f7d6db07d51\\javafx-fxml-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-controls\\12.0.1\\838555460c025b8df0fbc0fa03bf3f3767698f89\\javafx-controls-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-controls\\12.0.1\\a6502999bdb947885c8d121a47d745d52a06577a\\javafx-controls-12.0.1.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-graphics\\12.0.1\\18a01ffdd4edb82e7da3bc99937c6a608d1eaaa6\\javafx-graphics-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-graphics\\12.0.1\\19ec56a15d7dd9c49112912547425b718485d7db\\javafx-graphics-12.0.1.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-base\\12.0.1\\6f5947b255bb26e072862b3bb0c32d54d8effe84\\javafx-base-12.0.1-win.jar;C:\\Users\\Ejer\\.gradle\\caches\\modules-2\\files-2.1\\org.openjfx\\javafx-base\\12.0.1\\6abae81d00e0c6f4fde02e2666f9c0b989ff47e4\\javafx-base-12.0.1.jar bfst20.addressparser.GUI
Successfully started process 'command 'C:\\Program Files\\Java\\jdk-11.0.4\\bin\\java.exe''
:run (Thread[Daemon worker,5,main]) completed. Took 28.566 secs.


您可以手动创建以下配置来启动您的 JavaFX 应用程序:https://prnt.sc/qyaos2 或单击 a€?runa€? Gradle 上下文选项卡中的任务:https://prnt.sc/qyas3y

文档中描述了启动 Gradle 任务的所有可能选项:https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html