Intellij is insisting that I add "requires rt" to my module-info.java file
I am on the latest version of Intellij as of the current date on os x.
I'm trying to create a java 9 module, and I have created a maven project. The maven project toolchain is a little complicated because I'd like to compile the jar for java 8 and have a java 9 compiled module-info file in the jar so it can interoperate with the module system if necessary.
To accommodate this, I have set up the pom file as per instructions on the maven site, and set the project language level in Intellij I set the language level to 9.
I then added a single interface, which references java.lang.FunctionalInterface and java.lang.Exception, and now Intellij is insisting that that I add "requires rt" to my module-info.java file.
As far as I can tell, there is no such thing as an "rt" module. For example, when I do this, then the project will not build under maven, as the compiler fails to recognize this module. e.g.
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /Users/bill/dev/rs/fnex/src/main/java/module-info.java:[2,14] module not found: rt
[INFO] 1 error
Additionally, the documentation for FunctionalInterface and Exception both state that they are in module java.base.
https://docs.oracle.com/javase/9/docs/api/java/lang/FunctionalInterface.html and https://docs.oracle.com/javase/9/docs/api/java/lang/Exception.html
Additionally, if I ask the JDK to list modules, there's no listing for an "rt" module.
If I remove "requires rt;" from my module-info.java file, the project builds via maven just fine, which leads me to believe that java.base is an implicit module dependency. However, doing this apparently leaves inteillij to believe that all things in the java.lang package are erroneous, and this is an unacceptable amount of noise. I may as well be using emacs at this point.
If I add "requires java.base;" to my module-info.java file, then the project builds just fine, but I still get lots of red errors in my code and also one in my module-info file.
If I set the language-level of the module down to 8, the errors in the code go away, but Intellij complains about the existence of module-info.java.
Is there something that I'm doing wrong? Is Intellij not doing the right thing here?
Please sign in to leave a comment.
I found the issue.
In project structure, for that module the language level was set to Java 9. However, In dependencies, the java version was set to eight. I changed the java version in dependencies to Java 9, and it is no longer confused regarding what should go into module-info.java.
I had not intentionally set the java 8 dependency, but discovered this by setting up another project from scratch and going over the differences.
The way Maven is configured, I believe it should be able to warn me if I attempt to include a Java 9 only feature in the project (other than module-info of course). It would be nice if Intellij had some way of coping with module-info in an otherwise Java 8 only module.
Also, apologies for mixing the words project and module here. They are entirely too overloaded at this point.
This is paintful, because now intellij is read the pom file, and automatically downgrading the module dependency to java 8, and then throwing errors because module-info exists.
I found this tip.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000347524/comments/360000030024
I hope it works.
No luck. it still chokes on the existence of module-info.java.
Error:(1, 1) java: modules are not supported in -source 1.8
(use -source 9 or higher to enable modules)
Despite having configured source level to be java 9. At least with the above settings it does not try to change the dependencies.
Bill,
Do you have "Use --release option for cross-compilation" option enabled in compiler settings?
Hello Yaroslav,
It was checked. I experimented with it a bit, and it did not seem to make a difference.
However, on that same dialog box (Preferences: Build Execution Deployment: Java Compiler), I set the Per-module bytecode version to 9 for the modules and that did the trick. I still have the custom JVM settings that I linked to earlier defined as well. I have not experimented with removing those yet.
Thanks!
Bill,
Are "Per-module bytecode"settings properly kept between Maven refreshes?
It appears to be so.
To test this, I changed the version of a dependency in the pom, and did the refresh at IntelliJ's suggestion. I confirmed that the settings were the same and built the project and ran tests. All was good.
I then reverted the dependency and repeated the process successfully.
Thanks!