如何避免Java中的“重复类”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6428992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:50:31  来源:igfitidea点击:

how to avoid "duplicate class" in Java

java

提问by Jus12

Suppose I have have a java project myProjectand am using an external library jar (someJar.jar), which has a class com.somepackage.Class1.class.

假设我有一个 java 项目myProject并且正在使用一个外部库 jar ( someJar.jar),它有一个类com.somepackage.Class1.class.

Now I find an updated version of Class1.javawhich fixes a bug in the original jar.

现在我找到了一个更新版本,Class1.java它修复了原始 jar 中的错误。

I include the new Class1.javain my source code under package com.somepackage

Class1.java在包下的源代码中包含新的com.somepackage

When I build the project (e.g., using Netbeans), there is a dist\myProject.jarwhich contains the classcom.somepackage.Class1.classand a dist\lib\someJar.jarwhich also contains a class with the same name.

当我构建项目(例如,使用 Netbeans)时,有一个dist\myProject.jar包含该类com.somepackage.Class1.class,一个dist\lib\someJar.jar还包含一个同名的类。

When I run the file (e.g, using java -jar dist\myProject.jar), the new version of Class1.classis used (as I want).

当我运行该文件(例如,使用java -jar dist\myProject.jar)时,将使用新版本的Class1.class(如我所愿)。

  1. How does Java decide which class file to run in case of such duplicates? Is there any way I can specify precedence ?

  2. Is there any 'right' way to avoid such clashes?

  3. In Proguard, when I try to compress my code, I get a duplicate classerror. How do I eliminate this?

  1. 如果出现此类重复,Java 如何决定运行哪个类文件?有什么办法可以指定优先级吗?

  2. 有什么“正确”的方法可以避免这种冲突?

  3. 在 Proguard 中,当我尝试压缩代码时,出现duplicate class错误。我该如何消除这种情况?

采纳答案by Reverend Gonzo

  1. Java decides which one to use based on the order of the classpath. List yours first and you'll be fine.

  2. The "right" way would be to fix the orignal source, but sometimes that's not always an option.

  3. I haven't used ProGuard, but I have re-jarred libaries before that had duplicate classes. The solution in my case was to tell Ant to ignore duplicate classes. I would assume ProGuard would have that support too.

  1. Java 根据类路径的顺序决定使用哪一个。先列出你的,你会没事的。

  2. “正确”的方法是修复原始来源,但有时这并不总是一种选择。

  3. 我没有使用过 ProGuard,但我已经重新 jarred 库,因为它有重复的类。我的解决方案是告诉 Ant 忽略重复的类。我认为 ProGuard 也会有这种支持。

回答by Jon Skeet

Can you not create an updated jar file which contains the bug fix? It's going to make things a lot simpler if you don't have two versions of the same fully-qualified class around.

你不能创建一个包含错误修复的更新 jar 文件吗?如果您周围没有同一个完全限定类的两个版本,这将使事情变得简单得多。

回答by Vanchinathan Chandrasekaran

1) Updated Jar is a better solution.

1) 更新的 Jar 是一个更好的解决方案。

2) Use a different class name. Is there a reason, why you want to use the same class name and same packing? I don't think there is a reason.

2) 使用不同的类名。有什么原因,为什么要使用相同的类名和相同的包装?我不认为有什么理由。

3) create a wrapper/ proxy class, that encapsulate all the calls to the jar and you can decide to call this new class that fixes the bug ( provided it has a different name and packaging)

3) 创建一个包装器/代理类,它封装了对 jar 的所有调用,您可以决定调用这个修复错误的新类(前提是它具有不同的名称和包装)