How JVM works when two same jar be included in classpath

前端 未结 4 508
难免孤独
难免孤独 2021-01-01 20:27

It was a mistake of my co-worker: there was a jar named test.jar and he has fixed a bug of it. Then he re-compile the code and built a new jar named testnew.jar The proble

4条回答
  •  生来不讨喜
    2021-01-01 21:23

    As far as I can tell, it isn't defined.

    Java has a pluggable classloader system, and thus the only way to know what will happen is to look at the documentation of the ClassLoader class, probably in particular the ClassLoader#findClass method, which doesn't define a behavior for this, and to look at the relevant sections of the JLS and JVM specs, neither of which seem to specify a constraint on class loaders in this regard. Thus, unless the behavior is documented by the class loader being used by your web container, you can't know for certain which class will be loaded.

    The odds are that the first one found that matches the binary name of the class will be the one loaded, but there's a big difference between behavior we suppose to be the case, and behavior that is specified and/or documented.

提交回复
热议问题