读取类的JAR版本 [英] Read the jar version for a class

查看:46
本文介绍了读取类的JAR版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Web服务客户端,我希望使用JAR文件中的Implementation-Title和Implementation-Version作为用户代理字符串。问题是如何读取JAR的清单。

这个问题已经被问了很多次,但是答案似乎不适用于我。(例如Reading my own Jar's Manifest)

问题在于,简单地读取/META-INF/MANIFEST.MF几乎总是产生错误的结果。在我的情况下,它几乎总是指JBoss。

https://stackoverflow.com/a/1273196/4222206中提出的解决方案 这对我来说是有问题的,因为您必须硬编码库名称来停止迭代,然后它仍然可能意味着同一个库的两个版本在类路径上,您只返回第一个-不一定是正确的-命中。

https://stackoverflow.com/a/1273432/4222206中的解决方案 似乎只使用jar://URL,这在JBoss中完全失败,在JBoss中,应用程序类加载器生成vfs://URL。

类中的代码是否有办法查找其自己的清单?

我尝试了上面提到的项目,这些项目似乎在从java命令行运行的小型应用程序中运行得很好,但是我想要一个便携的解决方案,因为我无法预测我的库稍后将在哪里使用。

public static Manifest getManifest() {
    log.debug("getManifest()");
    synchronized(Version.class) {
        if(manifest==null) {
            try {
                // this works wrongly in JBoss
                //ClassLoader cl = Version.class.getProtectionDomain().getClassLoader();
                //log.debug("found classloader={}", cl);
                //URL manifesturl = cl.getResource("/META-INF/MANIFEST.MF");

                URL jar = Version.class.getProtectionDomain().getCodeSource().getLocation();
                log.debug("Class loaded from {}", jar);

                URL manifesturl = null;
                switch(jar.getProtocol()) {
                    case "file":
                        manifesturl = new URL(jar.toString()+"META-INF/MANIFEST.MF");
                        break;
                    default:
                        manifesturl = new URL(jar.toString()+"!/META-INF/MANIFEST.MF");
                }


                log.debug("Expecting manifest at {}", manifesturl);
                manifest = new Manifest(manifesturl.openStream());
            }
            catch(Exception e) {
                log.info("Could not read version", e);
            }
        }
    }

代码将检测正确的JAR路径。我假设通过将url修改为指向清单会给出所需的结果,但我得到的结果是:

Class loaded from vfs:/C:/Users/user/Documents/JavaLibs/wildfly-18.0.0.Final/bin/content/webapp.war/WEB-INF/lib/library-1.0-18.jar
Expecting manifest at vfs:/C:/Users/user/Documents/JavaLibs/wildfly-18.0.0.Final/bin/content/webapp.war/WEB-INF/lib/library-1.0-18.jar!/META-INF/MANIFEST.MF
Could not read version: java.io.FileNotFoundException: C:UsershiranDocumentsJavaLibswildfly-18.0.0.Finalstandalone	mpvfs	emp	empfc75b13f07296e98content-e4d5ca96cbe6b35eWEB-INFliblibrary-1.0-18.jar!META-INFMANIFEST.MF (The system cannot find the path specified)

我检查了该路径,似乎连JAR的第一个URL(通过Version.class.getProtectionDomain().getCodeSource().getLocation()获得)都已经错误了。应该是C:UsersuserDocumentsJavaLibswildfly-18.0.0.Finalstandalone mpvfs emp empfc75b13f07296e98content-e4d5ca96cbe6b35eWEB-INFliblibrary-1.0.18.jar.

所以这甚至可能指向WildFly中的问题?

推荐答案

我似乎在这里找到了一些合适的解决方案: https://stackoverflow.com/a/37325538/4222206

因此,最终此代码可以(至少)在JBoss中显示JAR的正确版本:

this.getClass().getPackage().getImplementationTitle();
this.getClass().getPackage().getImplementationVersion();

希望我下次搜索时能找到这个答案.

这篇关于读取类的JAR版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆