16

I have follow this guide to setup JavaFX onto a Linux machine. First I have installed Java 11

asus@asus-pc:/usr/share/openjfx/lib$ java -version
openjdk version "11.0.3" 2019-04-16
OpenJDK Runtime Environment (build 11.0.3+7-Ubuntu-1ubuntu219.04.1)
OpenJDK 64-Bit Server VM (build 11.0.3+7-Ubuntu-1ubuntu219.04.1, mixed
mode, sharing)
asus@asus-pc:/usr/share/openjfx/lib$ 

Then I have installed OpenJFX from the command sudo apt-get install openjfx

 asus@asus-pc:/usr/share/openjfx/lib$ ls
 javafx.base.jar      javafx.graphics.jar  javafx.swing.jar
 javafx.controls.jar  javafx.media.jar     javafx.web.jar
 javafx.fxml.jar      javafx.properties    src.zip
 asus@asus-pc:/usr/share/openjfx/lib$ 

Then have created a library in Eclipse.

enter image description here

Then I have include it into my java project. I try to run this code:

package se.danielmartensson.start;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application{

    /*
     * Start the start(Stage front)
     */
    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage front) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("/JUSBPlotter/src/se/danielmartensson/fxml/front.fxml"));
        Scene scene = new Scene(root);
        front.setScene(scene);
        front.setTitle("Fracken");
        front.show();
    }

}

And I have change the run configurations to:

enter image description here

But when I compile the code. I get this errors:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x1ff6d2c7) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module @0x1ff6d2c7
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at se.danielmartensson.start.Main.start(Main.java:20)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
    ... 1 more
Exception running application se.danielmartensson.start.Main

Question:

Is there anyone where who know how to solve this error? I have setup JavaFX before, but this time, it won't work for me.

Edit 1: If I change the run configurations to:

--module-path="/usr/share/openjfx/lib" --add-modules=javafx.controls,javafx.fxml

I get this errors enter image description here

euraad
  • 2,467
  • 5
  • 30
  • 51
  • Duplicated of this question: [IntelliJ IDEA - Error: JavaFX runtime components are missing, and are required to run this application](https://stackoverflow.com/questions/52906773/intellij-idea-error-javafx-runtime-components-are-missing-and-are-required-t) – José Pereda May 25 '19 at 19:34
  • @JoséPereda I have tried that solution, but that did not work for me. – euraad May 25 '19 at 19:41
  • 1
    You might have other errors, but you _need_ to include `javafx.fxml` in your VM arguments. – José Pereda May 25 '19 at 19:42
  • 1
    @JoséPereda If I apply `--module-path="/usr/share/openjfx/lib" --add-modules=javafx.controls,javafx.fxml` to my run configures, the errors change. Now I get `Caused by: java.lang.NullPointerException: Location is required.` because it cannot find the .fxml file, even if I have paste the location in the code. – euraad May 25 '19 at 19:46
  • That's exactly what I said: you have other errors. But first thing first, add the FXML module. Then solve the next error: remove `/JUSBPlotter/src/` from the path to load your FXML file. – José Pereda May 25 '19 at 19:47
  • @JoséPereda Now it's working. How could you know that `/JUSBPlotter/src/` causing this problem? – euraad May 25 '19 at 19:49
  • 4
    You never add `src` to `getResource()`... as it starts looking for your file precisely from there... – José Pereda May 25 '19 at 19:51
  • If you remove `/JUSBPlotter/src/` from the resource path, you removed one letter too much. If you specify the resource path starting at the classpath root, the path needs to start with `/`; the following part matches the package structure with `.` replaced with `/` with the exception of the file extension. I do strongly recommend using a module yourself and adding the proper `requires` statements instead of using the vm arguments to add access to modules. – fabian May 25 '19 at 23:57
  • You should not need to specify the module path when starting the application. Eclipse will take care of that for you. – Johannes Kuhn Sep 18 '19 at 21:20
  • @JohannesKuhn Thank you for the information. – euraad Sep 18 '19 at 21:58

5 Answers5

13

Having the same issue with IntelliJ IDEA 2020.1 with JDK 14.

Finally resolved by adding a module-info.java like this one under src/main/java if you use maven:

module sample {
    requires javafx.controls;
    requires javafx.graphics;

    opens sample;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
igonejack
  • 2,366
  • 20
  • 29
6

Go to Run>Run Configurations then Arguments tab and go to VM Arguments and paste the following code to add modules "--module-path /path/to/lib --add-modules javafx.controls,javafx.fxml" remember to modify /path/to/lib to your path your library then click apply and you're set

cyrus korir
  • 61
  • 1
  • 2
5

Thank you igonejack. I did what you said but I faced some other exceptions after that, So I came up with below code:

module {pkg}{
   requires javafx.controls;
   requires javafx.graphics;
   requires javafx.fxml;

   exports {pkg of Application class};

   opens {pkg};
}

after that you need to rebuild your project probably because of some exception about Kotlin. Then I saw exception "location is not set". To solve this you must start fxml location with "/" such as:

App.class.getResource("/form.fxml");

Edit

I made a HelloWorld project at JavaFxHelloWorld using Gradle.

user2971399
  • 500
  • 6
  • 6
5

thanks, works for me as follows

public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

module-info.java:

module sample {
    requires javafx.controls;
    requires javafx.graphics;
    requires javafx.fxml;
    opens sample ;
}
sveta
  • 51
  • 1
  • 1
2

As marco-rosati proposed in #638 issue, if you are using maven you can simply add this plugin:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <argLine> --add-exports javafx.graphics/com.sun.javafx.application=ALL-UNNAMED </argLine>
    </configuration>
</plugin>
TimT
  • 31
  • 4