Two classes with same name in classpath

后端 未结 4 521
醉话见心
醉话见心 2020-12-29 11:18

If I have two classes with same name say Matcher.java in two different jar in my classpath which one will be picked up by JVM , is there anyway I can suggest JVM to pick a s

4条回答
  •  一个人的身影
    2020-12-29 11:47

    there is a way for you to specify where the class should be picked from.. you can create your own class loader which would load classes according to your requirement.

    you can use your class loaded in 2 ways

    1. Pass it as a parameter to jvm (java -Djava.system.class.loader =com.somepackage.YourCustomClassLoader com.somepackage.YourMainClass)
    2. Use the class loader programatically to load a specific class (refer the links provided).

    here are some useful links on class loading

    • Oracle - How to write your own class loader
    • A very nice post on writing custom class loader

提交回复
热议问题