3

I'm having a really hard time with some Java modules. I'm working on an old Java project which has been "migrated" to Java 11. Nowhere in the project is there any module-info.java file defined.

I'm getting compilation errors in Eclipse/VS Code, which look like:

The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml

I don't fully understand why it's causing the problem, but I added a module-info.java definition to the root of the module.

module com.company.app {
    requires java.xml;
}

And that compilation error went away. I now have visibility errors everywhere and many, many more than before.

I've started to fix the visibility errors with exports and imports entries as needed, but now I have a problem.

In one of the projects, there is a source and a separate source-test folder. I've defined a module definition in the source folder.

The code in the source-test folder is separate, but has the same package structure. The following code:

import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.experimental.categories.Category;

The import org cannot be resolved. (in the line of the import static).

The type org.junit.Test is not accessible (in the corresponding line)

The type org.junit.experimental.categories.Category is not accessible (once again, in the corresponding line.)

I don't want to add the junit dependency to the main project code, since it's a testing dependency. However, if I define another module-info.java module inside the source-test folder, it complains about the build path containing a duplicate entry 'module-info.java'.

How can the dependencies and modules be correctly defined?

Daniel Gray
  • 1,697
  • 1
  • 21
  • 41
  • 1
    See [Java 9 + maven + junit: does test code need module-info.java of its own and where to put it?](https://stackoverflow.com/q/46613214/5221149) – Andreas May 03 '19 at 15:41
  • In Eclipse, in the configuration of your project's **Java Build Path**, is the source folder `source-test` marked as **Contains test sources**? This enables some magic to apply different visibility rules to test sources. See https://www.eclipse.org/eclipse/news/4.8/jdt.php#jdt-test-sources – Stephan Herrmann May 03 '19 at 18:36
  • The original issue, "The package org.w3c.dom is accessible ...", is fishy. You shouldn't need to migrate your code to a module to get over that. Do you have a JAR file with org.w3c.dom classes on your class path? – Alan Bateman May 04 '19 at 16:51
  • It is fishy! I agree. I have the same issue for all packages inside the `java.xml` module. There is no JAR in the classpath with these, but there are several projects in the workspace. The "root" project if you will, has no compilation errors for any of the `java.xml` packages... but all the dependent projects do. I'm guessing that the unnamed module in the root project is being passed with a transitive read of the `java.xml` packages which then conflict with the `java.xml` linked to the dependent project. I have no idea how to fix it though. – Daniel Gray May 06 '19 at 13:37

0 Answers0