70

When referencing simple .jar files, Eclipse shows an error stating:

The package java.awt is accessible from more than one module: <unnamed>, java.desktop

This happens for instance when javax.awt or javax.swing is included in the .jar files.

The simplest example would be the following:

package test;

import javax.swing.JDialog;

public class Test {
    public static void main(String[] args) {
        new JDialog();
    }
}

Adding a .jar file to the classpath with only the folder structure javax/swing (no files needed) will cause the error to appear. I'm using JDK 10/12 (neither works). Setting the compiler compliance to 1.8 makes the whole thing work again. On another machine with Eclipse 2018-09 this works with compiler compliance set to 10.

I'm on Eclipse 2019-03, on a (for testing purposes) freshly installed Eclipse 2018-09 it works fine. Why?

Edit June/2020 (Solution)

As the answers correctly stated, this is a restriction built into Java ages ago and only recently was forced upon us. I came into contact with it while migrating a big project with dozens of dependencies to Maven. There were libraries from around the year 2000! There were 'meta libraries' which consisted of several libraries packaged together. So there was no other way than to identify what was still needed (into the trash with the rest!), update libraries which violate the rules or find replacements for them. This took me many, many hours.

In the end it worked out and we've got a nice Maven project to work with.

Krann Sock
  • 933
  • 1
  • 7
  • 12
  • 4
    That's a restriction by the [Java Platform Module System (JPMS)](https://en.wikipedia.org/wiki/Java_Platform_Module_System), not by Eclipse (so don't shut the messenger). If you delete the file `module-info.java` in your default package (which disables JPMS) it should work with Java 9 or higher. – howlger Apr 08 '19 at 11:02
  • 6
    This happens when not creating the module-info.java. – Krann Sock Apr 08 '19 at 11:19
  • I uploaded a video showing my problem on a freshly installed 2019-03: https://youtu.be/6fQ8ZPprVyo – Krann Sock Apr 08 '19 at 11:29
  • I see. Solves moving the _JRE System Library_ from the _Modulepath_ to the _Classpath_ your issue? – howlger Apr 08 '19 at 11:41
  • As you can see at roughly 34 seconds into the video, it's inside the classpath already (I never had it anywhere else). Funnily, after moving it to the module path, the errors disappear. This does not work on the real project, however, where I originally found the error. – Krann Sock Apr 08 '19 at 11:45
  • 2
    Everything must be on the classpath, the JAR and the _JRE System Library_ (the video shows only the JAR being on the classpath). – howlger Apr 08 '19 at 11:47

16 Answers16

56

This is caused by

  • a JAR on the Classpath that contains the package java.awt that also exists in the system library but the
  • JRE System Library is on the Modulepath

In the Java Platform Module System (JPMS) it is not allowed to use the same package in more than one module. If the Modulepath and the Classpath is used, everything on the Classpath is handled as the <unnamed> module (in your case the package java.awt exists in the system module java.desktop and also via the JAR on the Classpath in the module <unnamed>).

Since the JRE System Library cannot be moved from the Modulepath to the Classpath (see this answer by Stephan Herrmann for details), you only have the following options:

  • Set the compiler compliance to 1.8 (as you already mentioned)
  • Rebuilt the JAR to avoid Java system library package names inside the JAR (if reflection is used, additional code changes may be necessary):
    • If you have the source code, change the package names (e.g. change the package and subpackae java to java_util and javax to javax_util) and recreate the JAR
    • If you have only the .class files you have to decompile the .class files first
howlger
  • 31,050
  • 11
  • 59
  • 99
  • It's not possible to add the JRE to the Classpath. – Krann Sock Apr 08 '19 at 13:24
  • @KrannSock I have extended my answer by how this is possible. – howlger Apr 08 '19 at 13:53
  • @howlher No matter what you do - it gets added back to the Modulepath. – Krann Sock Apr 08 '19 at 14:07
  • @KrannSock Yes, you're right. As far as I know, this was possible in previous versions. Is repacking the JAR an option for you? – howlger Apr 08 '19 at 15:12
  • I could repack them manually, there are like 5 of them. Any instructions on what do do with those? Or do you mean just changing the folder/package names inside the jar? They are all very old libraries from ~2004. Is it not possible to use (partial) package paths of the JRE System Library any more in a jar file? To be honest, I also don't see a reason for this to be necessary - but apparently those libraries do :-) – Krann Sock Apr 08 '19 at 18:56
  • @KrannSock Yes, change the folder/package names inside the JAR. `javax.swing.foo` would work, but I would not recommend it. You should not use package names that are used by others, especially not of the standard library. Previously this was only a recommendation. Since Java 9 (with JPMS) this can cause problems like yours. – howlger Apr 08 '19 at 19:05
  • Even javax.swing.foo doesn't work (this is the case in one of those libraries where it's javax.swing.xyz with only .gifs in it - you better don't ask...). If you add the explanation to your answer I'll accept it. Thank you very much! – Krann Sock Apr 08 '19 at 19:21
  • @KrannSock I've edited my answer. Tell me if anything is missing. – howlger Apr 08 '19 at 20:41
  • I have this problem BUT both the JRE System Library and the referenced library JAR files are on the Classpath, nothing is on the modulepath. FML. – Neil Bartlett Nov 22 '19 at 20:59
  • @NeilBartlett In Java 9+ the _JRE System Library_ is per se on the _Modulepath_, not on the _Classpath_ (I have corrected my answer accordingly): see [this answer by Stephan Herrmann for details](https://stackoverflow.com/a/53824670/6505250). What compiler compliance level have you set in your project? Which Java version and which Eclipse version do you use? – howlger Nov 23 '19 at 11:40
  • 1
    @howlger Thanks for the link. Unfortunately adding module-info.java is not a practical solution because as soon as you do this, you require all dependencies of the project to be modules, or at least to have automatic module names. Many of my dependencies do not have the Automatic-Module-Name header so I have to use the filename. So `com.google.guava_21.0.0.jar` becomes `requires com.google.guava.21.0.0` but this is a syntax error in the module-info! Catch 22... – Neil Bartlett Nov 25 '19 at 09:04
  • @howlger You said that the JRE System Library is "per se on the modulepath, not the on the classpath". Maybe there is a bug in Eclipse 2019-06 because the JRE System Library is clearly shown under classpath in the Java Build Path properties page, NOT under modulepath. – Neil Bartlett Nov 25 '19 at 09:10
  • @NeilBartlett In Eclipse 2019-09 I was not able to reproduce this. The _JRE System Library_ is shown on the modulepath, cannot be moved to the classpath (like a JAR) and even after directly editing the `.classpath` file (removing the line ``) the _JRE System Library_ is still shown on the modulepath. [Please upgrade](https://wiki.eclipse.org/FAQ_How_do_I_upgrade_Eclipse_IDE%3F#Always_enable_major_upgrades) and try again. – howlger Nov 25 '19 at 14:53
  • Hi, is there any command (maybe for maven) which lists me the dependencies which add these packages? I would like to exclude them manually since they seem to be broken for Java11. – Baradé Nov 15 '21 at 15:35
33

Since I'll bet lots of people will be running into this problem with modular Java, I'll help and give the real answer.

This error happens when

  • you have a dependency in your project
  • that contains code
  • using packages
  • that are also in the modules
  • being referenced by your project

If your project has set the source compatibility to something like Java 12, it will start enforcing the rule, that has been there all along in Java:

"Don't use packages that belong to the JDK in your own code."

Unfortunately, lots of developers and vendors have done that over the years. Can't do that anymore.

If you set your project to Java 12 source compatibility, Eclipse adds the JDK modules which include everything "java.*" and "javax.*" and even "jdk.*", "org.w3c.*". These packages may be in use by your dependencies or their transitive dependencies.

How to fix

You need to:

  • look at which package its complaining about
  • and expand the "Projects and External Dependencies" node in the Package Explorer.
  • Find out which dependency is using that package.
  • Then you can simply exclude that dependency from your project.

Or you could get the source of that dependency, if available, and rebuild the jar with changed packages. Otherwise you have to remove that dependency and find a replacement for that technology. Pain huh?

If its a transitive dependency you can often just exclude it. Here is an example of that for Gradle based projects.

GradleConfig.xml:

configurations {
   all*.exclude group: 'xml-apis'
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
K. Taylor
  • 431
  • 5
  • 8
  • "Don't use packages that belong to the JDK in your own code." what does this statement mean? Are you saying I cannot have a `package javax.matt`, because javax belongs to the jdk? – matt Jun 08 '20 at 06:35
  • 5
    That's right. For example Java "owns" the package "java" so "java.anything" would not be allowed. Same for "sun.anything", "javax.anything". – K. Taylor Jun 09 '20 at 13:45
  • 1
    import javax.xml.parsers.ParserConfigurationException; This statement is causing me the error. When I expand the "Projects and External Dependencies" I found xmlParserAPIs-2.6.2.jar is using the package javax.xml.parsers. So in build.gradle I did configurations { all*.exclude group: 'xmlParserAPIs' } but all in vain – Yatin Kanyal Sep 24 '20 at 04:54
  • 1
    Can you....explain what the answer means? Like...what is the problem? "This error happens when you have a dependency in your project that contains code using packages that are also in the modules being referenced by your project." What is a dependancy? How do i know if i have a dependancy? How do i know if a dependancy is in my project? How do i know if the dependancy in my project contains code? What is a module in my project? What does it mean for a module to reference a module? It sounds like if Java internally does `import java.io;` then my code is not allowed to call `import java.io;`. – Ian Boyd Jun 29 '22 at 20:45
  • 1
    Of all the answers, excluding xml-apis worked for me! – Wafa Saba Jan 05 '23 at 20:03
27

I found a simple solution to troubleshoot this in Eclipse.

  1. Hit Ctrl + Shift + T in Eclipse to open the Open Type prompt.
  2. Type the name of the package that is causing the issue. For me, it was org.w3c.dom
  3. The search results will show all the locations from where this package is being loaded.
  4. Remove every JAR from the classpath that comes in the result other than the JDK 11 library.

My project being a legacy one, I had to remove a lot of JARs from the build path. Also, my project does not use Maven. So removing the JARs was a fairly straightforward step. The steps might vary for other build tools like ANT, Maven, Gradle, etc. I have just explained the troubleshooting steps above.

  • 4
    Ctrl + Shift + T - works great. I was able to see that a jar was located in both the JRE and in an m2 repository. – Jack BeNimble Mar 30 '22 at 14:27
  • 3
    Ctrl + Shift + T was a lifesaver! Everyone explains the problems, but nobody explains how to find the dependency in question. – scho Aug 09 '22 at 07:49
  • 3
    This is much more helpful than the higher rated answers. – fakedad Mar 09 '23 at 10:28
26

In my case, it was because I included a dependency (Apache Tika) in the POM.xml file.

I had to force the exclusion of the module that contained the classes with errors while imported at that dependency:

    <dependency>
        <groupId>org.apache.tika</groupId>
        <artifactId>tika-parsers</artifactId>
        <version>1.24.1</version>
        <exclusions>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

It worked for me that way.

JZarzuela
  • 502
  • 4
  • 11
  • 1
    In my case the problem came with the dependencies for org.apache.xmlgraphics batik-transcoder 1.13 and batik-codec 1.13. The exclusion as you describe here (adapted to the batik dependencies) worked for me. – S. Doe Sep 17 '20 at 03:58
  • I had the same problem with Batik too. Thank you – Jean-Paul May 30 '23 at 15:32
12

I think my flavour of the problem might be useful.

I got this error for classes under javax.xml.stream, by old Maven projects that depend on artifacts like xml-apis, stax-api, or geronimo-stax-api.

Technically, the problem is what others have already said: those artifacts expose the javax.xml.* package without any awareness of Java modules (they were invented later), so the package is automatically assigned to the unnamed module, which conflicts with the same package being included in the JDK's most recent versions, where the package has its own module name, and therefore the same package results in two different modules, which is forbidden.

That said, the practical solution is essentially to work with Maven exclusions to remove those dependencies from your project and let it use the JDK version instead (or, of course, remove them as direct dependencies, if that's your case). Use the equivalent if you're working with another build system.

In theory, the more recent flavours of these packages offered by the JDK might be non backward-compatible, in practice, I doubt such JSR specifications changed much over the years and so far, I haven't seen any issue with their replacement.

zakmck
  • 2,715
  • 1
  • 37
  • 53
11

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

Disappointingly I don't see any compiler flags to show what jar the problem is with Even -Xlint:module doesn't seem to show up anything useful and eclipse doesn't shed any light on the issue

Instead to find where java.awt comes from I've been using this script:

mvn dependency:copy-dependencies -DincludeScope=test -DoutputDirectory=deps
for i in deps/*.jar; do if unzip -l $i| grep -q java.awt; then echo $i; fi ; done

Strictly you don't have to specify the scope test as that's the default but I've included it as you might want to use compile instead

MSillence
  • 553
  • 6
  • 8
7

I've found some interesting behaviour with Java 11 and the xmlbeans library. The xmlbeans library is a transitive dependency of Apache POI, a very popular library for working with Microsoft Office documents, it is used to handle the internal XML structures of the newer Office formats. I've tested it with Apache POI 3.9 and it works perfectly fine, despite the error shown by Eclipse. So, I guess in Java 11 this rule it's not fully enforced.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
lordscales91
  • 423
  • 1
  • 8
  • 22
  • 4
    Came here with exactly the same problem when updating to the 5.0.0 of Apache POI, adding `xml-apisxml-` to the poi-ooxml dependency did the trick – NeeL Jul 12 '21 at 14:48
2

For Apache POI version 5.0.0 using Maven, in the pom.xml:

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>5.0.0</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>5.0.0</version>
  <exclusions>
    <exclusion>
      <groupId>xml-apis</groupId>
      <artifactId>xml-apis</artifactId>
    </exclusion>
  </exclusions>
</dependency>

Fixed: "The package javax.xml.parsers is accessible from more than one module"

Christophe Roussy
  • 16,299
  • 4
  • 85
  • 85
2

I met a similar issue with the eclipse IDE while upgrading JDK from 1.8 to 11, "The package org.w3c.dom is accessible from more than one module: , java.xml". upgrading eclipse from 2019 to 2021 and setting system JDK home to 11 does not solve it. I don't think it's caused by "org.w3c.dom" existing in different jars of the classpath,dut to "Order and Export" should ordered the search sequence.

After several hours of searching and investigating, this issue is fixed by setting the Java Compiler - Compiler compliance level to 1.8(default is 11).

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Nov 29 '21 at 09:03
1

You can do what other people suggest which is to exclude xml-apis which worked fine with me, but if your are using and an old jaxb-api replace them with jakarta.xml.bind-api:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
</dependency>

and of course upgrade your jaxb-impl to match the same api:

<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.3</version>
</dependency>
Mahmoud
  • 9,729
  • 1
  • 36
  • 47
1

Steps below helped me:

Right click Eclipse project > Properties > Java Build Path In Libraries tab, remove all the external jar files under Modulepath and add them under Classpath (you can just select all the jars and drag them under Classpath) Click Apply and Close

desertnaut
  • 57,590
  • 26
  • 140
  • 166
1

Wow, this was a tough one!

Some helpful tips (a summary of my journey through this maze):

This started when I migrated from Java 1.8 to 11.0.11

Right out of the gate this caused problems. I needed to change the syntax for how to specify the Java version for the maven build plug in as shown below:

Before

        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>

After

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <release>11</release>
            </configuration>
        </plugin>       

After that, everything compiled at a command prompt. However, when opening the project in Eclipse, I got a bunch of errors as shown in the next section.

Getting "<class> cannot be resolved to a type" errors

When I got errors in Eclipse and not at the command line I immediately started looking at configuration settings in Eclipse to no avail. After some googling I came to this post. The post at https://www.eclipse.org/forums/index.php/t/1110036/ was also very helpful.

Finding what resources were the problem

The answer to this post by @Anu suggesting using <shift><ctrl>T to bring up the type search window was very helpful using this I was able to see what resources were causing the problem (i.e. any resource that is also included in the JDK, in my case it was the ErrorHandler in the xml-apis jar). Type the name of one of the resources giving a "cannot be resolved to type" to see the name of the jar file, in my case the jar is xml-apis-1.4.01.jar.

All of this is shown in the screen shot below (click on the image to get a cleaner view).

enter image description here

Finding the Dependency that contains the offending jar

We can now use the information from https://www.eclipse.org/forums/index.php/t/1110036/ to find the dependency in our pom.xml file that contains the offending resource.

Open the pom.xml file in Eclipse and select the "Dependency Hierarchy". In the filter type the name of the jar file we found in the previous step (in my case it was xml-apis).

This will pull up the dependencies creating the problem. A screen shot of the Dependencies Hierarchy filtered for "xml-apis". From this we can see the offending dependencies in our pom.xml file are xercesImpl and xlsx-streamer.

enter image description here

Solving the problem

We can now add exclusions to these dependencies that will resolve the problem as shown below.

    <dependency>
        <groupId>com.monitorjbl</groupId>
        <artifactId>xlsx-streamer</artifactId>
        <version>2.1.0</version>
        <exclusions>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
    </dependency>



    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>2.12.0</version>
        <exclusions>
            <exclusion>
                <groupId>xml-apis</groupId>
                <artifactId>xml-apis</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

And this fixes the current problem.

enter image description here

John
  • 3,458
  • 4
  • 33
  • 54
0

I had this problem with the Stanford Core NLP package. As mentioned above adding an exclusion in the pom.xml solved the problem:

    <dependency>
        <groupId>edu.stanford.nlp</groupId>
        <artifactId>stanford-corenlp</artifactId>
        <version>4.5.2</version>
        <exclusions>
           <exclusion>
             <groupId>xml-apis</groupId>
             <artifactId>xml-apis</artifactId>
           </exclusion>
        </exclusions>
     </dependency>
lejon
  • 1
  • 3
0

Configuration property was added to Eclipse. Full discussion here.

File: <workspace>\.metadata\.plugins\org.eclipse.core.runtime\.settings\org.eclipse.jdt.core.prefs
Property:
org.eclipse.jdt.core.compiler.ignoreUnnamedModuleForSplitPackage=enabled

Mike
  • 20,010
  • 25
  • 97
  • 140
0

For me adding JRE into classpath solved the problem.

project -> build path -> configure build path -> then adding JRE into class path

Abhishek Chauhan
  • 365
  • 3
  • 11
-1

First of all, make sure your pom is actually correct by running mvn install

if that works, and eclipse still complains look at build path and find the library it is holding onto that is not in your pom.

Sometimes I just

rm .classpath
rm -rf .settings
rm -rf project
mvn eclipse:clean eclipse:eclipse
desertnaut
  • 57,590
  • 26
  • 140
  • 166
user939857
  • 377
  • 5
  • 19