I am working on opensource project. As of of now I don't have any customization in any class. So using all the jars files provided by opensource project. My question is if I modify one java file, compile it and pack it new jar file with same folder structure, will there be any issue at start up of server or run time? If not which class file will be called(default file or my customize java class file)?
-
I believe the JVM will use the class from teh first jar file it can find in the classpath. And a similar question has been answered today itself "How JVM works when two same jar be included in classpath" – sachinrahulsourav Mar 18 '12 at 10:45
-
possible duplicate of [How JVM works when two same jar be included in classpath](http://stackoverflow.com/questions/9757279/how-jvm-works-when-two-same-jar-be-included-in-classpath) – T.J. Crowder Mar 18 '12 at 10:50
2 Answers
In fact it depends on many factors:
If both jar files are in the same ClassLoader, for instance the Java classpath (
-cp
option), normally it should be the first file found in the jar list order.If deployed in a JavaEE container, like in an EAR file or in
WEB-INF/lib
or a WAR file, there is no warranty the container will load the same class between two startups. In that context, the only thing sure is thatWEB-INF/classes
is searched beforeWEB-INF/lib
In a complex ClassLoader hierarchy, the default behavior is parent-first search but JavaEE implementations have introduced mechanisms like parent-last policy (WebSphere) or filtering thanks to deployment descriptors (WebLogic, JBoss/WildFly)
An option may be to declare jar file dependencies in META-INF/MANIFEST.MF
file thanks to the Class-Path
attribute. It should enforce a loading order at the ClassLoader level, particularly when started with java -jar myapp.jar
but it may depend on implementations in a JavaEE context.
Remark: when using an OpenSource project, it may be fair to submit a change request and publish your changes or improvements so that the community takes benefits from it. Then your project may update to main stream without such a difficulty of wild patches in your ClassPath.

- 10,217
- 2
- 38
- 77
-
As you said If both jar files are in the same ClassLoader, for instance the Java classpath (-cp option), normally it should be the first file found in the jar list order. But Again in second point, you said if it is the case with war file deployed in webcontainer like tomcat, which class file will be called is not guaranteed. Is that correct? – M Sach Mar 18 '12 at 11:48
-
One more point. You said An option may be to declare jar file dependencies in META-INF/MANIFEST.MF file thanks to the Class-Path attribute. It should enforce a loading order at the ClassLoader level but it may depend on implementations in a JavaEE context. Just to confirm that my understanding if class A is packed in two jar files J1 and J2. Class A is called from class B(packed in J3).Now if we want Class A from jar J2 to be called, then we need to mention this in menifest file of jar J3. Correct? – M Sach Mar 18 '12 at 12:07
-
Yes correct. J3 manifest must contain Class-Path: j2.jar and J2 manifest must contain Class-Path: j1.jar. So it enforces loading order first: j3, then j2, then j1. Particularly useful when packed with Main-Class attribute. – Yves Martin Mar 19 '12 at 07:53
-
In a WAR, you should also use Class-Path attribute in your jars files included in WEB-INF/lib to be (almost) sure about the search order. The easiest way is to put your "replacement" classes in WEB-INF/classes and the original jars in WEB-INF/lib – Yves Martin Mar 19 '12 at 07:56
Class loader is looking for the first place where needed resource is located. It means that if class with the same name and package appears in 2 jars the first found will be used. Which one is the first? According to the classpath: if for example class A appears in jars one.jar and two.jar and your command line is:
java -cp one.jar;two.jar
MyMain`
the version from one.jar will be used. But if command line is
java -cp two.jar;one.jar
MyMain`
the class from two.jar will be instantiated.

- 114,158
- 16
- 130
- 208
-
4Do you have a reference saying this is *specified/documented* behavior? I'm [not seeing one](http://stackoverflow.com/questions/9757279/how-jvm-works-when-two-same-jar-be-included-in-classpath/9757720#9757720). – T.J. Crowder Mar 18 '12 at 10:58
-
1what happens when it is `java -cp /src/lib/* MyMain` and the lib directory contains one.jar and two.jar? – enator Nov 03 '17 at 12:23
-
2I tested AlexR's saying on `openjdk 1.8` and he is right https://gist.github.com/nimatrueway/baecf702d04f2b2f26385a6e4da65212 – NiMa Thr Mar 10 '19 at 10:39