0

We're migrating from Java 8 to Java 11. We have a legacy project Y which depends on another legacy project X. The project X has no sources, it's just a collection of about 300 jars. The build is ant-based, no maven.

I cannot build the project Y now with JDK 11 (neither in Eclipse, nor externally) because it says:

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

I get this error in Eclipse on a line which does import org.w3c.dom.Document;

When I do an external build (with ant, outside of Eclipse) I can build successfully (with basically the same build.xml as under JDK 8). Why is only Eclipse complaining? Is it because of this javac bug which I reference below?

I was reading here (these are directly related to my issues):

but I am still unable to fix these build issues.

I think the clash is between org.w3c.dom package from some of these 300 jars and the same package in the JDK module java.xml.

The weird thing is that org.w3c.dom.Document doesn't seem to be present in any of these 300 jars, it's just that other classes from the same package are present in jars.

I cannot lightly change project X because it's a shared library used by multiple legacy projects. On the other hand, I cannot remove java.xml module from the build path of project Y. So I don't know how to approach this.

Isn't there some way to just say: OK, use the classes from the JDK first, then use those from the JARs (even though they share the same package, they are not the same class).

What is the right way to fix these errors?

10
  • @meriton I said I read this but I still see no solution. That's why I am asking. Commented Jan 17, 2022 at 15:58
  • Also, we don't need to know how many things you have tried, but which things you have tried. How else could we check whether you have already tried the solution we're about to describe? Commented Jan 17, 2022 at 15:59
  • @meriton OK, I tried things mostly in Eclipse, specifying different ways of making project Y depend on X (in the classpath, in the module path). Then changing various compiler options. Adding the 300 jars directly as JAR dependencies in project Y. Also tried turning project X into module, etc. etc. I cannot describe everything I've been doing 6-7 hours today. Commented Jan 17, 2022 at 16:00
  • So I am asking in general, seems with this structure things are not going to work at all. Right? Seems we need a more general rework of all this. Commented Jan 17, 2022 at 16:04
  • Well, that sounds like you haven't tried (3) solution from the answer I linked. So perhaps try that and report back? Commented Jan 17, 2022 at 16:05

1 Answer 1

3

Modularization

Starting from version 9 Java supports JPMS (Java Platform Module System). See overview by Oracle.

In the Java Platform Module System it is not allowed to use the same package in more than one module. So, the solution is to find modules (jars) that exports this package org.w3c.dom and to remove one of the modules or package in it.

Sign up to request clarification or add additional context in comments.

14 Comments

Thanks, can you elaborate on this part: "So, the solution is to find modules (jars) that exports this package org.w3c.dom and to remove one of the modules or package in it." I don't understand it. Note that I am still learning about JPMS, I am not very familiar with it.
@peter.petrov You have a project, that has N dependencies. Your project and each dependency are a jar module, (if we are in jpms). So, the problem is that two modules in this set export this package, that is not allowed. So, the problem is to find modules (jars) that have this package. Be careful. Your project can use libA as a dependency, bu libA can use libB that has this package. So, in your IDE you should check not only your dependencies, but also the dependencies of the dependencies etc. Something like this.
OK... let's say I have found these JARs which have org.w3c.dom. What do you mean then by "and to remove one of the modules or package in it." ?
There’s something apparently nobody has mentioned so far. Even if you manage to compile the code, it will not run unless you exclude java.xml from the environment, as otherwise, classes in the org.w3c.dom package will only be looked up in that module and nowhere else. But when you say you can’t exclude java.xml at build time (because it is used), you obviously can’t exclude it from the runtime either. That all assuming that these org.w3c.dom.X classes weren’t in the java.xml module anyway and hence, entirely redundant.
When I try to produce a similar scenario, javac does complain about the attempt to create, e.g. org.w3c.dom.Foo, and I have to pre-create it using Java 8 compliance level. Then, when this class does already exist, javac doesn’t complain about accessing org.w3c.dom.Document like Eclipse does but complains about accessing org.w3c.dom.Foo, just like the runtime would do. So it’s not as if javac was ignoring the issue. You have to deliberately adjust the options to get this code compiled.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.