简体   繁体   English

使用 Gradle 在 IntelliJ 中运行 JavaFX 项目

[英]Running JavaFX project in IntelliJ with Gradle

I have a problem running a Java program using JavaFX and Gradle, in the IntelliJ IDE.我在 IntelliJ IDE 中使用 JavaFX 和 Gradle 运行 Java 程序时遇到问题。 I've looked at many solutions to, what seems to be, the same problem, however none of these have worked.我已经查看了许多解决方案,似乎是同样的问题,但是这些都没有奏效。

This question: IntelliJ + Gradle + JavaFX building, but not running is very similar to the problems I'm experiencing, along with the fixes I've tried.这个问题: IntelliJ + Gradle + JavaFX building, but not running与我遇到的问题非常相似,以及我尝试过的修复。 However the solution did not work for me.但是,该解决方案对我不起作用。

Currently I'm trying to get a very simple application running:目前我正在尝试运行一个非常简单的应用程序:

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();
    }
}

This application does run, when using the commandline and 'gradlew run', however it doesn't run when using the IntelliJ IDEA IDE.该应用程序在使用命令行和“gradlew run”时会运行,但在使用 IntelliJ IDEA IDE 时不会运行。

What's weird is that if I remove the TextField 'input', the program runs no problem.奇怪的是,如果我删除 TextField 'input',程序运行没有问题。 This leads me to believe the problem having to do with the module java.controls.这让我相信问题与模块 java.controls 有关。 However this is imported in my build.gradle:但是,这是在我的 build.gradle 中导入的:

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
    }
}

These 2 errors appear when trying to run the program with the TextField:尝试使用 TextField 运行程序时会出现这 2 个错误:

运行程序时出错

When looking at the projects libraries, you get this overview:查看项目库时,您会得到以下概述:

项目库

Which clearly shows the libraries being present.这清楚地表明存在图书馆。

When running 'gradlew --info run' I get the following output:运行 'gradlew --info run' 时,我得到以下输出:

> 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\resources\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.

You may manually create the following configuration to start your JavaFX application: https://prnt.sc/qyaos2 or click the “run” task in the Gradle context tab: https://prnt.sc/qyas3y您可以手动创建以下配置来启动您的 JavaFX 应用程序: https : //prnt.sc/qyaos2或单击 Gradle 上下文选项卡中的“运行”任务: https : //prnt.sc/qyas3y

All possible options for launching Gradle tasks are described in the documentation:https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html文档中描述了启动 Gradle 任务的所有可能选项:https ://www.jetbrains.com/help/idea/work-with-gradle-tasks.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM