Java, Classpath, Classloading => Multiple Versions of the same jar/project

前端 未结 5 2034
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 12:13

I know this may be a silly question for experienced coders. But I have a library (an http client) that some of the other frameworks/jars used in my project require. But all

5条回答
  •  無奈伤痛
    2020-11-22 12:33

    Classloaders load class on demand. This means that the class required first by your application and related libraries would be loaded before other classes; the request to load the dependent classes is typically issued during the loading and linking process of a depending class.

    You are likely to encounter LinkageErrors stating that duplicate class definitions have been encountered for classloaders typically do not attempt to determine which class should be loaded first (if there are two or more classes of the same name present in the classpath of the loader). Sometimes, the classloader will load the first class occurring in the classpath and ignore the duplicate classes, but this depends on the implementation of the loader.

    The recommended practice to resolve such kind of errors is to utilize a separate classloader for each set of libraries that have conflicting dependencies. That way, if a classloader attempts to load classes from a library, the dependent classes would be loaded by the same classloader that does not have access to the other libraries and dependencies.

提交回复
热议问题