Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid "duplicate class" in Java

Tags:

java

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

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

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

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

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

  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 class error. How do I eliminate this?

like image 326
Jus12 Avatar asked Jun 21 '11 16:06

Jus12


1 Answers

  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.

like image 182
Reverend Gonzo Avatar answered Sep 23 '22 15:09

Reverend Gonzo