Java 11 unnamed module issue when using Maven model - java

So, I'm trying to migrate a tool from Java 8 to Java 11.
I did the first step to make it work without modules and the code compiles successfully.
Now I'm trying to add the module-info.java, and I first faced issues because of the libraries used in my tool. Some are already compatible with Java 11 (e.g. Lombok) but some others are not, so I'm trying to import them using the requires and the artifactId name.
But I seem stuck because of my Maven model & Maven model builder dependency as I get the following error when building:
[ERROR] the unnamed module reads package org.apache.maven.model.merge from both maven.model.builder and maven.model
[ERROR] module maven.model.builder reads package org.apache.maven.model.merge from both maven.model.builder and maven.model
[ERROR] module maven.model reads package org.apache.maven.model.merge from both maven.model.builder and maven.model
What should I do for this kind of error? It seems I need both (build still fails if I comment one or the other). Does it mean I cannot add modules to my tool because of my dependencies?
N.B.: The libraries are set to their latest version (i.e. 3.6.3)

I "fixed" this using the maven-exec-plugin as described in the answer to this question.
Edit: When running the main class through the maven-exec-plugin, the compiler does (for some reason unknown to me) not complain anymore about the conflicts.

Related

Unknown Java maven compilation error: R1549aadb

Whilst modularizing and migrating a Java 8 project to Java 17 an unknown maven error has been encountered.
The project has non-modular dependencies.
The regular classes and test classes (JUnit5) compile and run fine through Eclipse (without invoking maven).
The regular classes compile fine with maven, however when maven compiles the test classes (JUnit5) errors such as are reported:
[ERROR] /C:/some/path/to/a/source/file.java:[11,15] cannot access org
R1549aadb
Where org is assumed to be the first portion of a package name, other examples encountered include java, javax.
The line number always seems to correspond to an import statement. in this case line [11] was: import org.junit.jupiter.api.Assertions;
Anyone familiar with this kind of error?
Does anyone know what R1549aadb refers to?

Java Gradle Missing libraries Modue that exists or have been imported - > Task :compileJava

So I have been facing these issues in so many JavaFX Gradle based projects in Intelli J Idea IDE. This has pushed me to the point where I had to manually download library files and make them part of my projects as a workaround.
The gradle projects I have they keeping failing when ever i run the > Task :compileJava in the IDE, for example in this particular that made me create the issue is that i have successfully imported the socket io lib from maven implementation 'io.socket:socket.io-client:2.0.1' , i have managed to import it and write a bit of sample code for it and i have added
requires engine.io.client;
requires socket.io.client;
in the module info file . So when its time to run this fails stating that
error: module not found: socket.io.client
requires socket.io.client;
error: module not found: engine.io.client
requires engine.io.client;
I have tried on JDK 13,16,17 to see if I am missing something but keeps on failing to run , so I have noticed now as a trend in my previous JavaFX project in which i managed to get away with.
So if there is anyone who understands what's wrong with Gradle set up please help.
This answer outlines an approach rather than a concrete solution.
socket.io.client and engine.io.client are not module names.
The socket.io-client library is not Java platform modularized (as far as I can tell), so it will be an automatic module.
The name of the module will be derived from the jar name. I don't know the exact translation as the jar name has . and - characters which may be remapped (or not) to make the module name valid. Try first the exact jar file name. There can be only one module per jar.
Additionally to requiring the right name, the jar needs to be on the module path. Maven will do this automatically for automatic modules, Gradle will not. I am not a Gradle expert, so will not provide advice on how to do that for Gradle.
If you use the right name in module-info and ensure the jar is on the module path, then it may work, or it may be incompatible with the Java module system in ways that are not easily fixable by you (i.e. the broken module must be fixed by the module maintainers).
You can raise an issue for the library maintainer for them to create module-info.java files for the modules and update their documentation on how to use their libraries in a Java module environment.
If the library you are trying to use is incompatible with the Java module system when used as a module, then you could try making your project non-modular by deleting module-info.java from your project and adding appropriate command-line switches. To understand how to do this, refer to documentation on non-modular projects at openjfx.io.

How to identify in Eclipse PDE the plugin or dependency that provides a package?

I am facing many instances of the following error message in the process of migrating an Eclipse plugin from Java 8 to Java 11. The build is using Tycho, but I'm not sure that matters much.
The package org.xml.sax.helpers is accessible from more than one module: <unnamed>, java.xml
The package javax.xml.parsers is accessible from more than one module: <unnamed>, java.xml
…
The message is explicit. The solution I consider is excluding the dependency that causes the duplication since this is now provided by the runtime environment.
My problem is that the target platform that I am using includes hundreds of JAR files and I can't sift through the uncompressed version of each file to find out which one provides the packages.
If I could identify the dependency, or have the Java compiler write a more detailed error message, that would help me solve the issue.
Therefore, I used https://github.com/javalite/jar-explorer to search my Maven dependencies and the plugins folder of my target platform (Eclipse 2020-09), to no avail.
Is there in PDE a possibility to identify the plugin that provides a specific Java package?

How to depend on java validation api /hibernate modules in java with gradle as build tool?

I have similar question as here. However that question does not mention what build tool he is using and I assume that he is using maven as I didn't have problems with maven when using java 9 modules with it previously.
I am using the hibernate validator and I want to use java 9 modules, so I added a module-info file to the package of the module where I am depending on the validator api (the classes I am using are Validator, ValidatiorFactory , ... from packages like javax.validation)
I searched for these classes and found that they reside in this jar in my project dependencies: validation-api-2.0.1.Final.jar, the classes I am using are inside package validation.
I used the command jar --file=/path-to-the-jar-on-my-pc/validation-api-2.0.1.Final.jar --describe-module in the terminal and got the names of the modules exported from that jar:
No module descriptor found. Derived automatic module.
java.validation#2.0.1.Final automatic
requires java.base mandated
contains javax.validation
contains javax.validation.bootstrap
contains javax.validation.constraints
contains javax.validation.constraintvalidation
contains javax.validation.executable
contains javax.validation.groups
contains javax.validation.metadata
contains javax.validation.spi
contains javax.validation.valueextraction
So now when I put in my module-info file for example requires javax.validation the IDE complains that module is not found . I even added the dependency manually in the project structure (pressing ctrl+shift+alt+s to access it in intellij) where I added it from the path where it is stored in my machine but still same result.
I also tried the help tool from intellij and I found that it added requires java.validation; to my module-info and not requires javax.validation;, but anyway neither of them work.
I searched in pom.xml of that module and found this element <Automatic-Module-Name>java.validation</Automatic-Module-Name>, so now I am almost sure that gradle is causing the problem but I am no expert in gradle and how building tools work, so how can I solve this with staying at using gradle as build tool?
Gradle didn't add proper support for the Java Platform Module System until version 6.4. However, you have to explicitly configure Gradle to work with modules.
// build.gradle
java {
modularity.inferModulePath = true
}
Though if I'm not mistaken, inferModulePath is true by default as of Gradle 7.0.
For more information regarding Java modules and Gradle see https://docs.gradle.org/current/samples/sample_java_modules_multi_project.html
I had the same issue. Changing javax.validation to jakarta.validation resolved it.
build.gradle.kts:
implementation("jakarta.validation:jakarta.validation-api:3.0.1")
module-info.java:
requires jakarta.validation;
I am using gradle java modularity plugin
https://plugins.gradle.org/plugin/org.javamodularity.moduleplugin
here is what I set on my build.gradle.kts:
java {
modularity.inferModulePath.set(false)
}
application {
// Define the main class for the application.
mainModule.set("modules.demo.gradle.consumer")
mainClass.set("com.demo.gradle.consumer.Consumer")
}
I know modularity.inferModulePath.set(false) contradicts what Slaw wrote, but it is necessary if you use the plugin. See documentation
https://github.com/java9-modularity/gradle-modules-plugin

Eclipse module problems jdk 10

I'm having a new problem this morning with my project (it worked fine on Friday). Eclipse seems to be having trouble figuring out it's module / class path, and nothing I'm trying seems to affect it. I'm using the following Eclipse configuration:
Version: 2019-03 M1 (4.11.0 M1)
Build id: 20190117-2133
Oracle JDK 10.0.2
My project is a Java Maven multi-module (those are maven modules, not java modules) project, but does not define any java modules itself (we haven't converted it yet). When attempting to compile it, eclipse throws up its hands and sends the message
"The package java.sql is accessible from more than one module: <unnamed>, java.sql".
While, on the surface, this seems helpful, I cannot find another module that defines the java.sql package, and the only instance of a class from the package (e.g.: java.sql.ResultSet) exists within the JDK system supplied library. This also happens with the javax.xml package with the same error.
I've attempted closing and re-opening eclipse, performing a clean, and a maven update project for everything, to no avail. I've inspected the eclipse .log file, and there's nothing useful in there. Is there anyway to figure out what other module Eclipse thinks it's pulling that package from?
My next step is to delete the entire project and re-import it.
UPDATE: I think I may have found the offender... there's a dependency that declares a couple of classes into the java.sql package, and that's probably borking things up. The specific JAR is net.sourceforge.hatbox:hatbox:1.0.b8, and is a transitive dependency of the com.h2database:h2:1.4.197 jar. Is there any way to tell eclipse to ignore the package from that JAR?
Update 2: Build Path:
I'm not sure I count this as a true solution, but I was able to find a workaround to my problem. I downgraded eclipse from the installed version listed in the question (4.11.0) to the base photon install (4.8.0), and I can now compile and work again.

Categories