15

I am building java jar file using ant. I need to include additional jars using "zipfileset src="xxx.jar" "zipfileset src="yyy.jar" and both xxx.jar and yyy.jar have the classes with the SAME fully-qualified class names. So the resulting jar file has duplicate class names. What are the possible implications of having duplicates?

Thank you.

0

5 Answers 5

20

If they're duplicate implementations, nothing–it wouldn't matter which is loaded.

If not, you're at the mercy of class load order, and may get a different version than you want.

It is specified that classpath entries will be searched in the order listed (as per this classpath doc). but that's only relevant if you're in complete control of classpath creation (unlike in a web app, for example).

(With the caveat that classpath wildcarding makes the order non-deterministic.)

0
5

In general this situatioin is highly non recommended and should be avoided.

Jars in java are just containers for your class files. java uses classloaders that look at the classpath and load class files from there. so if you have 2 jars A.jar and B.jar that have the same class x.y.Foo inside, the class from the jar that comes first in the classpath will be loaded. So, if your classpath is A.jar,B.jar (in this order) the class Foo from A.jar will be used in runtime. This inconsistency can lead to very hard-to-fix bugs from my experience

1
  • I see. I will change ant build file to include "dir" attribute in zipfileset instead of "src". Jan 24, 2012 at 22:05
1

whats mean duplicated? it's obvious that you can not have 2 classes with the same name in the same package (even your project will not compile), but if you mean that you have 2 classes with the same name in different packages that's "is ok".

2
  • I have 2 jars: xxx.jar and yyy.jar that are used separately. They are partially sharing exact implementations. In addition I am building another jar that includes these 2 jars. So if I open the resulted jar then I can see duplicate class names in the same package. Jan 24, 2012 at 22:00
  • question, both classes has the same functionality?, because if that's true , you could point only to one jar and avoid that, example : yyy.jar(remove class, and use xxx.jar{keep class})
    – elopez
    Jan 27, 2012 at 17:42
0

I agree with Dave.

Can you separate them by namespace to avoid the pitfalls he suggests?

0

Another answer which I don't see discussed here is that if you plan on signing your jars, aars, apks then it signjar will complain that you have duplicate entries

jarsigner: unable to sign jar: java.util.zip.ZipException: duplicate entry: com/foo/bar/baz.java

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.