5

I have a problem running a Java program using JavaFX and Gradle, in the IntelliJ IDE. 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. 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.

What's weird is that if I remove the TextField 'input', the program runs no problem. This leads me to believe the problem having to do with the module java.controls. However this is imported in my 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:

Errors when running the program

When looking at the projects libraries, you get this overview:

Project libraries

Which clearly shows the libraries being present.

When running 'gradlew --info run' I get the following output:

> 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.
SBK
  • 81
  • 2
  • 10
  • 1
    This is a duplicate of [JavaFX 11: IllegalAccessError when creating Label](https://stackoverflow.com/questions/54291958/javafx-11-illegalaccesserror-when-creating-label/54292408#54292408), at least regarding the error you get running from your IDE. – José Pereda Feb 05 '20 at 18:02
  • It very much is yes. However it seems, that solution focuses on how to install JavaFX and add the libraries/modules to IntelliJ manually, which to my knowledge, should be handled by Gradle. – SBK Feb 05 '20 at 18:11
  • 1
    Well, that is the case, _when you use Gradle_. However, when you run through IntelliJ, you _don`t use Gradle_. – José Pereda Feb 05 '20 at 18:12
  • So in order for me to be able to run the program through IntelliJ, to use the debugger and such, I would need to also install JavaFX manually? Alternatively, is there a way to configure IntelliJ, so that it uses the JavaFX modules from Gradle? – SBK Feb 05 '20 at 18:16
  • 1
    There’s a gradle window where you can select the tasks to run, and you can create a gradle configuration that you will run from IntelliJ instead of the default one, – José Pereda Feb 05 '20 at 18:19
  • Do you know of somewhere, with documentation on how to do that? – SBK Feb 05 '20 at 18:30
  • 2
    https://openjfx.io/openjfx-docs/#IDE-Intellij, section Non-modular with Gradle, Run the project shows one option. For the other one, just go to Run->Edit Configurations...->Add New Configuration (select Gradle template, select the project and add the run task). – José Pereda Feb 05 '20 at 18:36
  • Awesome! Thank you very much. Adding the configuration was what worked. You could add this as an answer, which I will mark as correct for others to see. – SBK Feb 05 '20 at 18:47

1 Answers1

2

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

All possible options for launching Gradle tasks are described in the documentation: https://www.jetbrains.com/help/idea/work-with-gradle-tasks.html

Egor Klepikov
  • 3,386
  • 5
  • 19
  • 34