Home > Enterprise >  IntelliJ New JavaFX Project dosent work - InvalidModuleDescriptorException
IntelliJ New JavaFX Project dosent work - InvalidModuleDescriptorException

Time:11-07

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?

CodePudding user response:

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).


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.
  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.

Recommendations

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 Mavaen Gradle 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 fixes suggested in this answer, or perhaps look at some other info at openjfx.io or review some open source JavaFX projects which use Gradle and perhaps make some more custom changes.

Or, look at a demo project using TornadoFX, which is a JavaFX Kotlin based framework. TornadoFX has a quick start, which may help, but I have not used it (however, I think it is Maven based not Gradle based).

  • Related