有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

获取当前jar文件名的java

我有一段代码,它在dev环境中返回信息。 但当我从jar运行时,代码并没有遵循它应该遵循的方式

jar的名称是硬编码的,并且希望得到它的名称,因为版本不同

private static String getManifestUrlForClass(Class<?> cl) throws URISyntaxException, IOException {

        URL url = cl.getResource(cl.getSimpleName() + ".class");
        String s = url.toString();

        System.out.println("URL Path: " + url.getPath());
        System.out.println("URL File: " + url.getFile());

        String path = MYCLASS.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        String revisionNumber = "";
        String decodedPath = "";

        JarFile jarfile = null;


         try {
        decodedPath = URLDecoder.decode(path, "UTF-8").replace("classes", "");

         try {
        jarfile = new JarFile(decodedPath + "MYJAR-ver.si.on.jar"); 
         } catch (IOException e1) {

         System.out.println("or Path to file cannot decode...");
         e1.printStackTrace();
         }

        Manifest manifestFromJar = jarfile.getManifest(); //

        System.out.println("Manifest from " + jarfile.getName().toString() + " = "
                + manifestFromJar.getMainAttributes().getValue("Revision-Number").toString()); 

        revisionNumber = manifestFromJar.getMainAttributes().getValue("Revision-Number").toString();
        } catch (IOException e) {
         System.out.println(url.getFile().toString() + "is not jar");// TODO Auto-generated catch block
         System.out.println("or Path to file cannot decode...");
         e.printStackTrace();
         }

        return revisionNumber;

    }

MYJAR永远都是一样的,只是| ver。硅。on |很可能会有所不同,硬编码名称并不是最佳做法

我想做什么? 1.拿上我的罐子。硅。在…上罐子的位置,不管它在哪里 2.使用该位置访问其清单 3.使用清单提取修订号 4.在ui中显示修订号

我对java还不熟悉,对它不太了解。我读过一些关于使用“rsrc:”来访问jar的内容,或者类似于https://stackoverflow.com/a/40680501/6756124的内容


共 (0) 个答案