This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if two jars contains same classes (but different class implementation), will they comflict?

 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two jar have same class files, but their classes have different interfaces.

if i add both jar files to my jboss lib directory, the app calls one of the class in one jar,

will the class be located? will compiler confuse?
 
Greg Charles
Sheriff
Posts: 2987
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class from whichever jar comes first on the classpath is the one that gets used. The other one might as well not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this, but sometimes you can't.
 
Chrix Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greg Charles wrote:The class from whichever jar comes first on the classpath is the one that gets used. The other one might as well not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this, but sometimes you can't.


Hi, if

class A in jar 1 have method foo()

and

class A in jar 2 have method foo(String param)

when

we call A.foo("haha"), can the second one be called?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the second method isn't found, the class will not compile.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are deploying this in something like JBoss you have configurable, hierarchical class loaders at your disposal. If you really need to do this, you might try investigating these.
 
Kevin Workman
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chrix Wu wrote:will the class be located? will compiler confuse?


What happened when you tried?

This isn't just with external JARs. For example, there are two List classes: java.util.List and java.awt.List. If you import both List classes, what happens when you create a variable of type List? Is there a way to have two different variables, one of each type?

You can figure these out by writing an extremely simple program and playing with the import statements.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic