3

I am trying to create a JavaFX application with IntelliJ and Java 11.

I can compile the application correctly, but when I try to debug it, I get the following 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: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 @0x7920ba90) 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 @0x7920ba90
    at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
    at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
    at sample.Main.start(Main.java:13)
    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$$$capture(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)
    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 sample.Main

I have creted the project by creating a new JavaFX Project, then I added a framework (Maven) and added the following to my POM file:

<dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11</version>
        </dependency>
    </dependencies>

This is my project .iml file:

<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11">
    <output url="file://$MODULE_DIR$/target/classes" />
    <output-test url="file://$MODULE_DIR$/target/test-classes" />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
      <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
      <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
      <excludeFolder url="file://$MODULE_DIR$/target" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-controls:win:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-fxml:win:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-graphics:win:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-base:11" level="project" />
    <orderEntry type="library" name="Maven: org.openjfx:javafx-base:win:11" level="project" />
  </component>
</module>

What else should I do, or what am I doing wrong?

Naman
  • 27,789
  • 26
  • 218
  • 353
Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56
  • From the `IllegalAccessError` message I'd guess `javafx.fxml` is not on the modulepath whereas `javafx.graphics` is. – Slaw Dec 24 '18 at 00:33
  • Are you sure you added the `javafx.fxml` module properly. It should include the module name in the stacktrace. (`at javafx.fxml/javafx.fxml.FXMLLoader....`) – fabian Dec 24 '18 at 00:37
  • @Slaw how can I confirm that? – Miguel Mesquita Alfaiate Dec 24 '18 at 00:38
  • @fabian what does properly mean? I added that dependency within the pom file and intellij does the import automatically. how should I do it? – Miguel Mesquita Alfaiate Dec 24 '18 at 00:39
  • Can you show your `module-info` file? – Slaw Dec 24 '18 at 00:40
  • @Slaw I updated the question with the contents of the .iml file. was that what you were referring to? – Miguel Mesquita Alfaiate Dec 24 '18 at 00:42
  • That's actually not what I was referring to. If your own code is modular you'll have a `module-info.java` file. I was wondering if you added the appropriate `requires javafx.fxml` directive. However, if your code is not modular you won't have that file. I'm not terribly familiar with how Maven handles JPMS modules (or a mix of modules and non-modules) though so I don't know how much help I can be. – Slaw Dec 24 '18 at 01:03
  • @Slaw no, I don't have that file. I don't mind doing this in a modular way if it fixes this... I have been struggling with this for a few days now. Have a JavaFX application that has been compiling and now it doesn't. I guess something between updating java and intellij messed this up, so I am just trying to get a simple javafx application 11 running before getting back to the existing project. no luck so far... – Miguel Mesquita Alfaiate Dec 24 '18 at 01:15
  • @BlunT For 1. make sure your IntelliJ is compatible with Java 11. (which is not what the error reads though) ... and then 2. Make sure in your `Project Settings > Libraries` you have added the `sdk/lib` folder for JavaFX dependencies. 3. Then further ensure adding the VM arguments `--module-path "path to sdk/lib" --add-modules=javafx.controls,javafx.fxml`. 4 Try to run the program and if the error occurs still, do share how the command line under your IntelliJ terminal look for that run. – Naman Dec 24 '18 at 02:17
  • Have you checked [this tutorial](https://openjfx.io/openjfx-docs/#IDE-Intellij) (section Non-modular with Maven)? How do you create the JavaFX project? – José Pereda Dec 24 '18 at 11:23
  • @JoséPereda I have followed that tutorial and the HelloWorld using maven, but I might have missed something. I have been able to get this up and running with Java 10, but not with Java 11 so far. – Miguel Mesquita Alfaiate Dec 31 '18 at 17:15
  • You say "when I try to debug it", but does it run fine and you get this exception only debugging? – José Pereda Dec 31 '18 at 17:28
  • @JoséPereda no, same exception when running, debugging, or generating an exe artifact. – Miguel Mesquita Alfaiate Dec 31 '18 at 17:35
  • Hard to say without your code/project. Is it possible that you can share it? – José Pereda Dec 31 '18 at 17:47
  • @JoséPereda yes I can. I will leave a link here. Just can't do it now, don't have it here with me, only at the office. – Miguel Mesquita Alfaiate Dec 31 '18 at 18:15
  • Check out this other thread: https://stackoverflow.com/questions/52140346/java11-javafx-and-maven-will-not-run-outside-of-netbeans-ide-9 – taciosd Jan 21 '19 at 19:04

2 Answers2

1

You can't avoid having a module.info file for javaFx 11. Check this other post: Can't get JavaFX to run in Eclipse

The error is similar.

Barosanu240
  • 725
  • 8
  • 15
0

Add a module-info.java under main/java if you are using JDK 9+.

module sample {
    requires javafx.controls;
    requires javafx.graphics;
}
igonejack
  • 2,366
  • 20
  • 29