Can I determine the version of a Java library at runtime?

前端 未结 3 541
南笙
南笙 2021-02-13 23:01

Is it possible to determine the version of a third party Java library at Runtime?

3条回答
  •  礼貌的吻别
    2021-02-13 23:28

    As I used to be tasked doing this for a lot of very legacy Java projects, the answer is "it can be done, but how to do it depends".

    First, check your JAR MANIFEST.MF file. Sometimes you get very lucky.

    Second, scan the JAR file for a version field. Sometimes you get lucky, and sometimes that value will lie.

    Third, scan the included properties files. There was a common ANT build pattern to keep the version in a property file (where it was easier to update).

    Fourth, start downloading the available JAR files for that project. Occasionally the version number is truly lost, and the only way to validate it is a specific version is to find a known old version and do a JAR to JAR comparison.

    There are other techniques, but these 4 cover nearly all scenarios. For some of the very poorly named niche libraries, it can be quite a challenge.

提交回复
热议问题