Reputation: 241
Java 9 / 10. I have been struggling with a simple project for more than a week.
As you can see in the picture, I want to use commons-collections as an automatic module (I have tried to add it with maven but that did not work out well).
So, I have red that I need to put the jar onto the module-path. Where does IntelliJ take this modulepath from? How can I tell the IDE to add commons-collections into the project so that
1. the compiler can find it at compile time and
2. Maven can find it at build time?
Anyone can help?
EDIT: I have tried to add it in the project-structure dialog as a module dependency in all kinds of different combinations. I have literally tried hundreds of things, moved the jar around in the structure and I cannot find a simple enough doc to tell me how to do this. I have used compiler options to add "--module-path automatic" (module specific and general compile options) in order to make IDEA find the thing and let Java make an automatic module out of it.
Upvotes: 4
Views: 2039
Reputation: 894
Intellij uses a module path if you run a program from an (intellij) module containing module-info.java, otherwise it will use a classpath.
I tried importing common4 as a module, it does seem to work for me, but I had to use a different 'requires' argument as compared to yours. Your 'requires' is 'commons.collections4', mine is 'org.apache.commons.collections4' (check the commons4 manifest entry for the highlighted Automatic-Module-Name and use that instead).
If the Automatic-Module-Name is missing from the commons4 manifest (it is absent from version 4.1 and earlier), Java may not detect the jar as a module if the name contains digits or illegal characters. Some maven repository jars therefore will not work and intelli will not see those jars as modules.
You can also check for a bad filename by using the following command:
jar --file=/path/to/jar --describe-module
If the command fails, it's likely that the jar does not have an Automatic-Module-Name entry and that the filename is poorly named.
ok
jar --file=C:\temp\jigsaw1-1.0.jar --describe-module
bad
jar --file=C:\temp\jigsaw1.0.jar
--describe-module`
'jigsaw1.0: Invalid module name: '0' is not a Java identifier'
Some maven jars may therefore fail to be detected as modules as they tend to look similar.
Upvotes: 1
Reputation: 45329
You need to add a library entry first, to make it available under Modules
:
Step 1: Add a library (Add -> Java -> jar file)
Step 2: Select the module (remember to click "Apply")
After that, the module-info.java
file will be successfully validated:
Upvotes: 4