简体   繁体   中英

Intellij IDEA - problem with JavaFX compilation

I've reinstall my OS from Win7 to 10. After reinstalling Intellij IDEA + Corretto_11 + JavaFX libary I cannot compile programa that was working.

I'm quite green in Java, so i don't know hot to approche the problem.

Project set to Correto_11, language level 11, and FX libary added to module.

Even code from Course does not compile.

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 @0x3a0bd002) 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 @0x3a0bd002
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at main.Main.start(Main.java:18)
    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.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    ... 1 more
Exception running application main.Main

Process finished with exit code 1

Edit:

After adding module I have following error:

"C:\Program Files\Amazon Corretto\jdk11.0.7_10\bin\java.exe" --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\lib\idea_rt.jar=50795:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1.1\bin" -Dfile.encoding=UTF-8 -p C:\Users\Sebastian\Documents\javafx_11\lib\javafx.base.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.graphics.jar;C:\Users\Sebastian\Desktop\JavaProjects\JavaFX_ToDo\out\production\JavaFX_ToDo;C:\Users\Sebastian\Documents\javafx_11\lib\javafx-swt.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.controls.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.fxml.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.media.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.swing.jar;C:\Users\Sebastian\Documents\javafx_11\lib\javafx.web.jar -m JavaFX.ToDo/main.Main
Error occurred during initialization of boot layer
java.lang.module.FindException: Error reading module: C:\Users\Sebastian\Desktop\JavaProjects\JavaFX_ToDo\out\production\JavaFX_ToDo
Caused by: java.lang.module.InvalidModuleDescriptorException: Package JavaFX.ToDo not found in module

Process finished with exit code 1

Edit 2:

Project structure: 在此处输入图像描述

Edit 3: Adding how my module look like:

module JavaFX.ToDo {
    requires javafx.graphics;
    requires javafx.controls;
    requires javafx.fxml;

    opens JavaFX.ToDo;
}

Edit 4:

Adding main content:

package main;

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

import java.io.IOException;



public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("mainwindow.fxml"));
        primaryStage.setTitle("JavaFX: ToDo-App");
        primaryStage.setScene(new Scene(root, 900, 500));
        primaryStage.show();
    }




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

    }
    @Override
    public void stop() throws Exception {
        try {
            TaskData.getInstance().storeTasks();

        } catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void init() throws Exception {
        try {
            TaskData.getInstance().loadTasks();

        } catch (IOException e){
            System.out.println(e.getMessage());
        }
    }

}

Or i can push all on my github.

https://github.com/s-osyra/JavaFX_ToDoList

The first error happened because your module wasn't open, and the javafx.fxml module (specifically FXMLLoader) wanted to use reflection.

The second error's just because you're using opens incorectly. It's meant to open packages, not modules.

This is your module-info file now:

module JavaFX.ToDo {
    requires javafx.graphics;
    requires javafx.controls;
    requires javafx.fxml;

    opens JavaFX.ToDo; //Here's the error
}

This is incorrect, because you don't have a package called JavaFX.ToDo , it's your module name. To make your module open (so javafx.fxml can do reflective access operations), you need to declare your module open like this:

open module JavaFX.ToDo {
  ...
}

xor this:

module JavaFX.ToDo {
  ...
  opens main; //This is where Main.java is, and where you use FXMLLoader
}


If you want it to be more secure, you could also declare it like this:

module JavaFX.ToDo {
  requires javafx.fxml;
  opens main to javafx.fxml; //Only fxml has reflective access to your package now
}

I would suggest reading this article about Java 9's module system. Scroll down to where they talk about open modules.

Side note: Module names are lowercase by convention. You probably want something like javafx_todo

I solved this as follows in IntelliJ: Run -> Edit configuration -> Configuration tab -> add following in VM options: --module-path c:\javafx-sdk-14.0.1\lib --add-modules=javafx.controls,javafx.fxml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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