Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
304 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...