Skip to content
Advertisement

Java Project Build and run problem with FX

i’m trying run a project with this architecture…
Project architecture

And, when i press “Run” button the code gives me this error…

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:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:901)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:196)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module @0x709a3ba3) 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 @0x709a3ba3
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2135)
    at ui.Main.start(Main.java:32)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    ... 1 more
Exception running application ui.Main

My code of my Main…

package ui;

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

import java.io.IOException;

public class Main extends Application {

    private static final String FOLDER = "fxml/";

    private FIBAManager manager;
    private MainGUIController MGC;
    private EmergentGUIController EGC;

    public Main() throws IOException {
        manager = new FIBAManager();
        EGC = new EmergentGUIController(manager);
        MGC = new MainGUIController(manager, EGC);
    }

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage window) throws Exception {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource(FOLDER + "MainWindow.fxml"));
        fxmlLoader.setController(MGC);
        Parent root = fxmlLoader.load();
        Scene scene = new Scene(root, null);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        window.setScene(scene);
        window.setTitle("");
        window.show();
    }

}

And i have this run cofiguration:

--module-path "C:UsersGiovaDownloadsjavafx-sdk-17.0.0.1lib" --add-modules=javafx.controls

I need help to solve this problem and run the project please

Advertisement

Answer

You are using fxml so you need to add the fxml module, otherwise it will not be visible for use:

--add-modules javafx.controls,javafx.fxml 

See the openjfx.io documentation titled Run HelloWorld using JavaFX SDK.

Example execution command for Linux from that source (modified to add the javafx.fxml module):

java --module-path $PATH_TO_FX --add-modules javafx.controls,javafx.fxml HelloFX

Alternately, you can define a module-info.java file which requires javafx.fxml and any other modules required for your application. See: Understanding Java 9 modules.

module <your module name> {
    requires javafx.controls;
    requires javafx.fxml;

    opens <your package> to javafx.fxml;
    exports <your package>;
}

Substituting your info for:

  • module <your module name> any name you want, but best to put it as the name of the primary package of your app, e.g. com.example.javafx.myapppackage. Follow Java 9 module naming conventions.
  • opens <your package> to javafx.fxml a package which uses fxml (opens is required to allow the fxml package to reflect on your code).
  • exports <your package> a package you want to make visible (via export) to users of your application’s module code; e.g. the package which contains the class in your appliction with a main(args) method.

Or, you can ignore the module system using the hack provided in mipa’s answer, as mipa does have a very good point about the additional complexity involved in using the module system. It is not a supported configuration by the JavaFX developers, but will likely work OK for your app.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement