Search code examples
kotlingradleintellij-ideajavafx

IntelliJ New JavaFX Project dosent work - InvalidModuleDescriptorException


I just installed: IntelliJ IDEA 2021.2.3 (Community Edition) and jdk-16.0.2

In IntelliJ I create a new project:

File->New->Project

Then select:

Language: Kotlin,

Build system: Gradle,

Project SDK, 16 version 16.0.2

Everything else I leave as is.

It creates the project.

I go to HelloApplication.kt and hit 'play' next to main() function.

I get this error:

Error occurred during initialization of boot layer java.lang.module.FindException: Error reading module: C:\Users\User\IdeaProjects\demo\build\classes\java\main Caused by: java.lang.module.InvalidModuleDescriptorException: Package com.example.demo not found in module

Can anyone guide me how to get it working? And why the project doesn't work out of the box?


Solution

  • As noted by Andrey in comments, this appears to be an known bug:

    The root cause of the issue is explained in the attached youtrack case.

    It would appear that this is a general issue with Kotlin+Gradle in Idea when using the Java Platform module system, and that it is not a specific issue just with the code created by the Gradle+JavaFX+Kotlin "New JavaFX Project" wizard (which is what I had assumed it was).

    Recommendations

    The projects generated in Idea 2023.3.3 for the new project wizard JavaFX+Gradle+Kotlin combination break when run in the IDE by clicking the green arrow. Those projects will still run if you use Gradle to run them (in the IDE go to the Gradle pane and double-click on Tasks | application | run). That might be an adequate work-around for many people.

    Hopefully, these are just short-term issues which Intellij will address in due course.

    I tried a new project in Idea with Kotlin+Maven instead of Kotlin+Gradle and it worked without having to manually delete the build directory after every execution. I suggest, if you are just experimenting, then just go with a Kotlin+Maven+JavaFX combo, at least until the Kotkin+Gradle+JavaFX wizard is working better.

    If the build system is important to you and should be Gradle, then, you will need to do some more work on your own for now. Try some of the work-arounds suggested in other answers to this question, or perhaps look at some other info on getting started with JavaFX at openjfx.io.

    Or, if you must use Gradle and JavaFX and Kotlin and Idea's built-in build and execute functionality rather than Gradle's, and are unable to get that to work in your IDE -> then convert your Kotlin JavaFX project to a non-modular project, as suggested in this answer by deleting the module-info.java and placing JavaFX on the module path by setting VM options that point to the SDK (as defined in the openjfx.io getting started documentation for JavaFX).


    I tried it in my environment (OS X), following the same steps and versions as you provide in your question, and I get the same result as you.

    IntelliJ IDEA 2021.2.3 (Community Edition) currently has some bugs and mistakes in the way it generates and uses JavaFX+Gradle+Kotlin projects using the Idea New JavaFX project wizard.

    The appropriate way to handle these would be to create bug request for IntelliJ in the Idea issue tracker.

    1. A new project (created as described in the question) will work the first time you try to run it then, it will fail with the error (also as described in the question):

      Error occurred during initialization of boot layer java.lang.module.FindException: Error reading module: C:\Users\User\IdeaProjects\demo\build\classes\java\main Caused by: java.lang.module.InvalidModuleDescriptorException: Package com.example.demo not found in module

      • To get around this you can manually delete the build directory every time before you attempt to run your application in the IDE.
      • UPDATE: I tried using this workaround recently, with Idea 2023.3.3 with JDK and JavaFX 21.0.2, and it did not work anymore. I would still get module errors even if I deleted the build directory.
    2. When the project runs, it doesn't setup the module path correctly and runs JavaFX from the classpath which will generate the following warning:

      (WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @4a072657').

      • It is a warning and your application will still function however that is not recommended. You can either ignore (at your own risk) or fix the runtime configuration to include the JavaFX modules on the module path.
      • See info in the following question about setting a module path:
    3. Another issue I had is that if you just use the defaults, if there is already a project named demo, it tries to create one named demo1 with packages and module names including that string, leading to an inspection warning:

      Module name component 'demo1' should avoid terminal digits

      • That is just another issue with the wizard, it is not related to your specific problem. For more info see #VersionsInModuleNames.
      • When you use the wizard, make sure you change any default project name, which is used to create the module name, to a valid name (e.g. one which does not end in a digit) before creating the project.