6

Context: Trying to create a simple JavaFx application using OpenJdK11 & OpenJFx11

Issue: I get an error as below when I try to execute

Error: JavaFX runtime components are missing, and are required to run this application

I referred to Link1 & Link2 . I also referred to 'Getting started with JavaFx11' - Link As suggested in the getting started when I try specifying the run configuration I get a message as shown

run' in 'build' cannot be applied to '(groovy.lang.Closure)' less... (Ctrl+F1) 

Image

Hope the issue faced is clear & await inputs as to where I am going wrong. (using IntelliJ ide)

Code:

Main -

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/Sample.fxml"));
    StackPane stackPane = new StackPane(root);
    Scene scene = new Scene(stackPane);
    primaryStage.initStyle(StageStyle.TRANSPARENT);
    primaryStage.setScene(scene);
    primaryStage.show();

}
}

FXML-

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns="http://javafx.com/javafx"
            xmlns:fx="http://javafx.com/fxml"
            fx:controller="Controller"
            prefHeight="400.0" prefWidth="600.0">

</AnchorPane>

Gradle-

plugins {
id 'java'
}

group 'testJavaFx'
version '1.0-SNAPSHOT'

sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.openjfx:javafx-base:11:win'
    compile 'org.openjfx:javafx-controls:11:win'
    compile 'org.openjfx:javafx-fxml:11:win'
    compile 'org.openjfx:javafx-graphics:11:win'

    testCompile group: 'junit', name: 'junit', version: '4.12'
}

compileJava {
    doFirst {
        options.compilerArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls'
        ]
    }
}

run {
    doFirst {
        jvmArgs = [
                '--module-path', classpath.asPath,
                '--add-modules', 'javafx.controls'
        ]
    }
}
iCoder
  • 1,406
  • 6
  • 16
  • 35
  • 1
    Add the `application` plugin to your build file, and you also need to add `javafx.fxml` to `—add-modules` – José Pereda Sep 30 '18 at 13:28
  • @JoséPereda thanks, but even after addition of {javafx.fxml} to {--add-modules} still same error. I even added base & graphics, but still same error. Anything else am doing wrong? Edited : Added the modules to compileJava & run, still error – iCoder Sep 30 '18 at 14:09
  • Did you add the application plugin? Can you run from a terminal `./gradlew run`? – José Pereda Sep 30 '18 at 14:19
  • I have added the Application plugin that removed the run in build cannot be applied to message. But the error on execution still persists. If I run "gradlew run " from terminal in Intellij it returns gradlew is not recognized. If I run "gradle run" it says build failed... Execution failed for task : run – iCoder Sep 30 '18 at 15:08
  • 1
    `gradlew` will only work if you're using the Gradle wrapper. Also, did you define `mainClassName` for the `application` plugin? – Slaw Sep 30 '18 at 15:12
  • Ok, got that working now with gradle run specifying the mainClassName. But when I try executing the prog. from IntelliJ it still gives the error. Anything I am doing wrong from IntelliJ ? – iCoder Sep 30 '18 at 15:14
  • How are you launching the app from Intellij? You should be using a Gradle configuration to execute the `run` task. – Slaw Sep 30 '18 at 16:49
  • I click on Run (Shift + F10) in IntelliJ from the Main class. This method was working till JdK10. If I use the Gradle Run task from IntelliJ it works but wondering why the error if I use the Run from IntelliJ, any setting to be changed in IntelliJ? – iCoder Sep 30 '18 at 23:10

1 Answers1

17

I have found a solution from the official repository of JavaFX: https://github.com/javafxports/openjdk-jfx/issues/236

There are some easy workarounds though. For example, you can have a main class that is not extending javafx.application.Application, and that main class can then call the main(String[]) method on your real main class (that way, the Java launcher doesn't require the javafx libraries to be available as named modules).

I created a java artifacts with Intellij IDEA and it works (without using gradle).

  • Thanks for that. Have adopted the main class calling the class which extends application. But will eventually move to gradle run task once project is ready. – iCoder Oct 05 '18 at 12:23
  • In my case I went modular and added `requires javafx.controls;` to my `module-info.java`, and it worked straigh after that. – SurfMan Jan 04 '19 at 09:07