Roxana
Roxana

Reputation: 1581

Classloading problems for the same java code, two .class files?

If I have a java class and: - I compile the class and include it in a jar, A - compile separately the same class and include it in a different jar, B (I know it's not politically right to do this...etc) (the compilation is done against the same jdk, on the same machine)

If I put these two jars in the same war - can I get class loading problems?

Upvotes: 0

Views: 59

Answers (2)

Hot Licks
Hot Licks

Reputation: 47699

Two ways to get into trouble:

  1. Have two externally different classes by the same name, such that other classes that are compiled against one will not be valid referencing the second.
  2. Have two identical copies of the class (or even the same copy) and manage (through one of several means) to load it twice with two different class loaders.

But having the same (from an external attributes standpoint) class twice in your classpath is not a problem -- the first one in the JAR search order will always be loaded.

Upvotes: 1

evanchooly
evanchooly

Reputation: 6233

No. You'll simply get the first copy it finds. If they're in the same package, you'll effectively never see that other class.

And it's not "politically" wrong to do this. It's fundamentally a bug.

Upvotes: 1

Related Questions