eclipse java 混淆,Eclipse被导入(“可从多个模块访问”)混淆了。

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

The package java.awt is accessible from more than one module: , 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.

解决方案

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 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 ).

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值