0

If the same version or different versions of jars are present in both .m2 folder and in java classpath, In which path java first tries to find the jar? and which path will be the highest priority?.

Raki
  • 361
  • 2
  • 13
  • 5
    This doesn't make any sense: Java doesn't just arbitrarily decide to search the `.m2` folder for JARs. It will take whatever is on the classpath and nothing else. So if you have version 1 on in .m2 and version 2 somewhere on the classpath then only version 2 will be loaded. The jar files in .m2 will be used *by placing them on the classpath*. – Joachim Sauer Jan 11 '21 at 13:17
  • Thanks for your time and your answer. – Raki Jan 12 '21 at 11:07
  • @Joachim Sauer: You mean for eg: if I have a version 1 and version 2 both are in the classpath, then Java will consider version 2 right? for compiling and also for runtime. – Raki Jan 12 '21 at 12:10
  • 1
    No, that's not at all what I wrote or meant. If you have multiple versions of some classes on the classpath, then the one earlier on the classpath will be loaded. But this can still lead to classes in version 2 being loaded that are not present in version 1 and lead to incompatibilities. – Joachim Sauer Jan 12 '21 at 12:17
  • OK. Thanks, I understood. – Raki Jan 12 '21 at 12:27

1 Answers1

1

Java, as in the VM (java.exe) will search the classpath. That's where it ends. m2 is a maven thing. It might be popular, but it is not built in or presumed by the VM to exist.

The m2 directory structure is such that it cannot be on the classpath itself, in fact.

The idea is that maven and other tooling will parse the dependencies and then craft a classpath that includes exactly the right required dependencies (by crafting a string with paths pointing into the .m2 directory), and then compiles using this crafted string as classpath, or if you use mvn test or whatnot, run a VM (java.exe) with this classpath. In those cases, the stuff in the .m2 dir is on the classpath. If you're just running java yourself (java -jar yourapp.jar, or java -cp your:dirs com.foo.MainApp or double clicking a jar), then .m2 is not involved whatsoever.

You can always write an app to check:

System.out.println(SomeClass.class.getResource("SomeClass.class"));

is code that will print a string that tells you where that class was loaded from. Works with (almost) anything, even String:

System.out.println(String.class.getResource("String.class"));

Thus, if you're not sure, that's one very easy to find out. Write a hello world app that does that for the type you're wondering where it's coming from.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Your answer is helped me to find the classes but,I am trying to find the jar path. For eg: This is my maven dependency org.apache.poi ooxml-schemas 4.1.2 How i could retrieve the path here ? do you have any idea please let me know. i tried with [https://stackoverflow.com/questions/15359702/get-location-of-jar-file/15359999] But not worked for me, I required like getPath(groupid/artifactId) = it shows the path where jar file is located. – Raki Jan 12 '21 at 11:33
  • (or) any other alternative also ok, atleast it has to show which version of jar is loading and from which path is loading.? Thanks for your time and answer in advance. – Raki Jan 12 '21 at 11:46
  • @Raki the jar filename _includes_ groupid, artifactid, version (GAV) unless you went out of your way to have maven make jar files that do not include this. Even if it does not, you can open that jar file (it's a zip), the manifest (META-INF/MANIFEST.MF, it's a file inside) will tell you. – rzwitserloot Jan 12 '21 at 14:10
  • I found out the answer to find the jar path. However, exactly not match with my asked question but there is a workaround with this code. Maybe, this was discussed already in a stack overflow for the comment question ``` String jarPath = ClassName.class .getProtectionDomain().getCodeSource() .getLocation().toURI().getPath();``` – Raki Jan 12 '21 at 17:19
  • That code is flimsy, long, and pointless: Just the URL from the class itself looks like e.g. `file:jar:/path/to/the/jarfile.jar!com/foo/ClassName.class` - the jar file is right in the middle. Just.. take eyeballs, point at screen, read it. – rzwitserloot Jan 12 '21 at 18:49