如何查看jar文件的版本?

发布于 2024-11-03 23:54:24 字数 134 浏览 9 评论 0原文

我目前正在开发一个 J2ME 抛光应用程序,只是对其进行增强。我发现很难获取 jar 文件的确切版本。 有什么方法可以找到类中完成的导入的 jar 文件的版本吗?我的意思是,如果你有一些东西,请导入 xyz;我们可以知道 xy 包所属的 jar 版本吗?

I am currently working on a J2ME polish application, just enhancing it. I am finding difficulties to get the exact version of the jar file.
Is there any way to find the version of the jar file for the imports done in the class? I mean if you have some thing, import x.y.z; can we know the version of the jar x.y package belongs to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

鹤舞 2024-11-10 23:54:24

解压 JAR 文件并查找清单文件 (META-INF\MANIFEST.MF)。 JAR 文件的清单文件可能包含版本号(但并不总是指定版本)。

Decompress the JAR file and look for the manifest file (META-INF\MANIFEST.MF). The manifest file of JAR file might contain a version number (but not always a version is specified).

分開簡單 2024-11-10 23:54:24

您需要解压缩它并检查其 META-INF/MANIFEST.MF 文件,例如

unzip -p file.jar | head

或更具体:

unzip -p file.jar META-INF/MANIFEST.MF

You need to unzip it and check its META-INF/MANIFEST.MF file, e.g.

unzip -p file.jar | head

or more specific:

unzip -p file.jar META-INF/MANIFEST.MF
孤独患者 2024-11-10 23:54:24

只是为了扩展上面的答案,在 JAR 中的 META-INF/MANIFEST.MF 文件中,您可能会看到一行: Manifest-Version: 1.0 ← 这是 NOT jar 版本号!

您需要查找 Implementation-Version,如果存在的话,它是一个自由文本字符串,因此完全取决于 JAR 的作者您将在其中找到什么。
另请参阅 Oracle 文档软件包版本规范

Just to expand on the answers above, inside the META-INF/MANIFEST.MF file in the JAR, you will likely see a line: Manifest-Version: 1.0 ← This is NOT the jar versions number!

You need to look for Implementation-Version which, if present, is a free-text string so entirely up to the JAR's author as to what you'll find in there.
See also Oracle docs and Package Version specificaion

淡水深流 2024-11-10 23:54:24

只是为了完成上面的答案。

清单文件位于 META-INF\MANIFEST.MF 路径下的 jar 内。

您可以在任何支持 zip 的归档程序中检查 jar 的内容。

Just to complete the above answer.

Manifest file is located inside jar at META-INF\MANIFEST.MF path.

You can examine jar's contents in any archiver that supports zip.

时光礼记 2024-11-10 23:54:24

每个 jar 版本都有一个唯一的校验和。您可以计算 jar 的校验和(没有版本信息)并将其与 jar 的不同版本进行比较。我们还可以使用校验和来搜索 jar。

请参考这个问题来计算校验和:
计算我计算机上文件的校验和的最佳方法是什么?

Each jar version has a unique checksum. You can calculate the checksum for you jar (that had no version info) and compare it with the different versions of the jar. We can also search a jar using checksum.

Refer this Question to calculate checksum:
What is the best way to calculate a checksum for a file that is on my machine?

放我走吧 2024-11-10 23:54:24

基本上,您应该使用 java.lang.Package 类,它使用类加载器为您提供有关类的信息。

示例:

String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();

我认为 logback 已知使用此功能来跟踪其生成的堆栈跟踪中每个类的 JAR 名称/版本。

另请参见 http://docs.oracle .com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779

Basically you should use the java.lang.Package class which use the classloader to give you informations about your classes.

example:

String.class.getPackage().getImplementationVersion();
Package.getPackage(this).getImplementationVersion();
Package.getPackage("java.lang.String").getImplementationVersion();

I think logback is known to use this feature to trace the JAR name/version of each class in its produced stacktraces.

see also http://docs.oracle.com/javase/8/docs/technotes/guides/versioning/spec/versioning2.html#wp90779

遗心遗梦遗幸福 2024-11-10 23:54:24

我想我会给出一个更新的答案,因为这个问题在搜索中的出现率仍然很高。

检查 CLi JAR 版本:

在 CLi jar 文件上运行以下命令:

unzip -p jenkins-cli.jar META-INF/MANIFEST.MF

示例输出:

Manifest-Version: 1.0
Built-By: kohsuke
Jenkins-CLI-Version: 2.210  <--- Jenkins CLI Version
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_144
Main-Class: hudson.cli.CLI

上面列出了 CLi 版本。

要获取服务器版本,请运行以下命令:(

java -jar ./jenkins-cli.jar -s https://<Server_URL> -auth <email>@<domain>.com:<API Token> version

以上内容将根据您的身份验证实现而有所不同,请相应更改)

示例输出:

Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider
INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider
2.210  <-- Jenkins Server Version

Thought I would give a more recent answer as this question still comes up pretty high on searches.

Checking CLi JAR Version:

Run the following on the CLi jar file:

unzip -p jenkins-cli.jar META-INF/MANIFEST.MF

Example Output:

Manifest-Version: 1.0
Built-By: kohsuke
Jenkins-CLI-Version: 2.210  <--- Jenkins CLI Version
Created-By: Apache Maven 3.6.1
Build-Jdk: 1.8.0_144
Main-Class: hudson.cli.CLI

The CLi version is listed above.

To get the Server Version, run the following:

java -jar ./jenkins-cli.jar -s https://<Server_URL> -auth <email>@<domain>.com:<API Token> version

(the above will vary based on your implementation of authentication, please change accordingly)

Example Output:

Dec 23, 2019 4:42:55 PM org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar getOrCreateProvider
INFO: getOrCreateProvider(EdDSA) created instance of net.i2p.crypto.eddsa.EdDSASecurityProvider
2.210  <-- Jenkins Server Version
狂之美人 2024-11-10 23:54:24

这个简单的程序将列出 jar 版本的所有情况,即

  • 在 Manifest 文件中找到的版本
  • 在 Manifest 中找不到版本,甚至从 jar 名称
  • 未找到 Manifest 文件

    Map<字符串, 字符串> jarsWithVersionFound = new LinkedHashMap();
    列表<字符串> jarsWithNoManifest = new LinkedList();
    列表<字符串> jarsWithNoVersionFound = new LinkedList();
    
    //循环遍历lib文件夹中的文件
    // 一个接一个地挑选一个 jar 并 getVersion()
    //在控制台中打印..保存到文件(?)..也许稍后
    
    File[] files = new File("path_to_jar_folder").listFiles();
    
    对于(文件文件:文件)
    {
        String 文件名 = file.getName();
    
    
        尝试
        {
            String jarVersion = new Jar(file).getVersion();
    
            if(jarVersion == null)
                jarsWithNoVersionFound.add(文件名);
            别的
                jarsWithVersionFound.put(文件名, jarVersion);
    
        }
        捕获(异常前)
        {
            jarsWithNoManifest.add(文件名);
        }
    }
    
    System.out.println("******** JAR 版本找到 ********");
    for(Entry jarName : jarsWithVersionFound.entrySet())
        System.out.println(jarName.getKey() + " : " + jarName.getValue());
    
    System.out.println("\n \n ******* 没有找到版本的 JAR *******");
    for(字符串 jarName : jarsWithNoVersionFound)
        System.out.println(jarName);
    
    System.out.println("\n \n ******* 未找到清单的 JAR *******");
    for(字符串 jarName : jarsWithNoManifest)
        System.out.println(jarName);
    

它使用 javaxt-core jar,可以从 http: //www.javaxt.com/downloads/

This simple program will list all the cases for version of jar namely

  • Version found in Manifest file
  • No version found in Manifest and even from jar name
  • Manifest file not found

    Map<String, String> jarsWithVersionFound   = new LinkedHashMap<String, String>();
    List<String> jarsWithNoManifest     = new LinkedList<String>();
    List<String> jarsWithNoVersionFound = new LinkedList<String>();
    
    //loop through the files in lib folder
    //pick a jar one by one and getVersion()
    //print in console..save to file(?)..maybe later
    
    File[] files = new File("path_to_jar_folder").listFiles();
    
    for(File file : files)
    {
        String fileName = file.getName();
    
    
        try
        {
            String jarVersion = new Jar(file).getVersion();
    
            if(jarVersion == null)
                jarsWithNoVersionFound.add(fileName);
            else
                jarsWithVersionFound.put(fileName, jarVersion);
    
        }
        catch(Exception ex)
        {
            jarsWithNoManifest.add(fileName);
        }
    }
    
    System.out.println("******* JARs with versions found *******");
    for(Entry<String, String> jarName : jarsWithVersionFound.entrySet())
        System.out.println(jarName.getKey() + " : " + jarName.getValue());
    
    System.out.println("\n \n ******* JARs with no versions found *******");
    for(String jarName : jarsWithNoVersionFound)
        System.out.println(jarName);
    
    System.out.println("\n \n ******* JARs with no manifest found *******");
    for(String jarName : jarsWithNoManifest)
        System.out.println(jarName);
    

It uses the javaxt-core jar which can be downloaded from http://www.javaxt.com/downloads/

滥情稳全场 2024-11-10 23:54:24

尝试以下两种方法

我迟到了,但您可以使用这些所需的类

import java.util.jar.Attributes;
import java.util.jar.Manifest;

这些方法让我访问 jar 属性。我喜欢向后兼容并使用最新的。所以我使用了这个,

public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
    String className = sourceClass.getSimpleName() + ".class";
    String classPath = sourceClass.getResource(className).toString();
    if (!classPath.startsWith("jar")) {
      // Class not from JAR
      return null;
    }
    String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + 
        "/META-INF/MANIFEST.MF";
    Manifest manifest = new Manifest(new URL(manifestPath).openStream());
    return manifest.getEntries().get("Build-Info");
}

public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
    Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);

    String attribute = version_attr.getValue(attributeName);

    return attribute;
}

当你使用maven并且需要已知类的pom详细信息时,这很有效。希望这有帮助。

I'm late this but you can try the following two methods

using these needed classes

import java.util.jar.Attributes;
import java.util.jar.Manifest;

These methods let me access the jar attributes. I like being backwards compatible and use the latest. So I used this

public Attributes detectClassBuildInfoAttributes(Class sourceClass) throws MalformedURLException, IOException {
    String className = sourceClass.getSimpleName() + ".class";
    String classPath = sourceClass.getResource(className).toString();
    if (!classPath.startsWith("jar")) {
      // Class not from JAR
      return null;
    }
    String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + 
        "/META-INF/MANIFEST.MF";
    Manifest manifest = new Manifest(new URL(manifestPath).openStream());
    return manifest.getEntries().get("Build-Info");
}

public String retrieveClassInfoAttribute(Class sourceClass, String attributeName) throws MalformedURLException, IOException {
    Attributes version_attr = detectClassBuildInfoAttributes(sourceClass);

    String attribute = version_attr.getValue(attributeName);

    return attribute;
}

This works well when you are using maven and need pom details for known classes. Hope this helps.

最佳男配角 2024-11-10 23:54:24

对于 Linux,请尝试以下操作:

find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "实现版本:" '{}' \;|awk -F ': ' '{print $2}'

For Linux, try following:

find . -name "YOUR_JAR_FILE.jar" -exec zipgrep "Implementation-Version:" '{}' \;|awk -F ': ' '{print $2}'

初见你 2024-11-10 23:54:24

如果你有winrar,用winrar打开jar,双击打开文件夹META-INF。将 MANIFEST.MFCHANGES 文件提取到任何位置(例如桌面)。

在文本编辑器中打开提取的文件:您将看到实现版本或发布版本。

If you have winrar, open the jar with winrar, double-click to open folder META-INF. Extract MANIFEST.MF and CHANGES files to any location (say desktop).

Open the extracted files in a text editor: You will see Implementation-Version or release version.

拍不死你 2024-11-10 23:54:24

[A] 我们可以手动提取/解压缩 jar 文件并检查 META-INF/MANIFEST.MF 文件中的 Bundle-Version 或 Implementing-Version 关键字。

[B] 在 Unix 环境中,
您可以轻松地将 grep 命令与 unzip 然后 cut 命令结合使用来获取版本。
[此解决方案确实假设 META-INF/MANIFEST.MF 中会提到版本。 (80% 机会)]

运行以下命令来获取版本。

VERSION=unzip -p "javaPackage.jar" "META-INF/MANIFEST.MF" | grep -m1 -E "捆绑版本|实现版本:" |切 -d':' -f2 | awk '{$1=$1;print}' 2>/dev/null;

说明:

  1. 解压->从 java jar 中获取 META-INF/MANIFEST.MF 文件的内容,这可以在不解压 jar 的情况下完成。
  2. grep->针对“Bundle-Version”或“Implementation-Version:”关键字
  3. 剪切搜索匹配的单词 ->根据连字符分割并得到第二部分
  4. awk ->修剪 jar-version
  5. 2>/dev/null 周围的空间 ->错误重定向到 /dev/null

注意: - 如果您想查找多个 jar 的版本,请运行 for 循环并调用上面的脚本。

[A] Manually we can extract/unzip the jar file and check Bundle-Version or Implementation-Version keyword in META-INF/MANIFEST.MF file.

[B] In Unix environment ,
you can easily use the grep command in combination with unzip and then cut command to get the version.
[This soluction does assume that there will be version mentioned in META-INF/MANIFEST.MF . (80% chances)]

Run below command to get the VERSION.

VERSION=unzip -p "javaPackage.jar" "META-INF/MANIFEST.MF" | grep -m1 -E "Bundle-Version|Implementation-Version:" | cut -d':' -f2 | awk '{$1=$1;print}' 2>/dev/null;

Explanation :

  1. unzip -> Get content of META-INF/MANIFEST.MF file from your java jar, This can be done without extracting the jar.
  2. grep -> Search matching word against either "Bundle-Version" or "Implementation-Version:" keyword
  3. cut -> Split based on hyphen and get 2nd part
  4. awk -> Trim space present around the jar-version
  5. 2>/dev/null -> Error redirection to /dev/null

Note : - If you want to find version of multiple jars , then run a for loop and call above script.

执笔绘流年 2024-11-10 23:54:24

从 MANIFEST 文件中过滤版本

您可以使用unzip -p my.jar META-INF/MANIFEST.MF | 。 grep '捆绑版本'

You can filter version from the MANIFEST file using

unzip -p my.jar META-INF/MANIFEST.MF | grep 'Bundle-Version'

秋千易 2024-11-10 23:54:24

不涉及提取 jar 文件的最佳解决方案是运行以​​下命令。如果 jar 文件不包含清单文件,您将收到“警告:未找到清单文件”

java -jar file.jar -v

best solution that does not involve extracting the jar files is to run the following command. If the jar file does not contain a manifest file you will get a "WARNING: Manifest file not found"

java -jar file.jar -v

秋意浓 2024-11-10 23:54:24

你可以执行这个脚本,它会在目录中找到所有版本的jar文件:

find /directory -maxdepth 3 -name "*.jar" | xargs -l % sh -c "JAR=\"\$(basename % .jar)"; VERSION=\"\$(unzip -q -c % META-INF/MANIFEST.MF | grep 'Implementation-Version' cut -d' ' -f2)\"; echo \"\$JAR \$VERSION\";" | sort

You could execute this script, it will find all versions of jar files in a directory:

find /directory -maxdepth 3 -name "*.jar" | xargs -l % sh -c "JAR=\"\$(basename % .jar)"; VERSION=\"\$(unzip -q -c % META-INF/MANIFEST.MF | grep 'Implementation-Version' cut -d' ' -f2)\"; echo \"\$JAR \$VERSION\";" | sort
℡寂寞咖啡 2024-11-10 23:54:24

只需使用 .zip 而不是 .jar 重命名扩展名即可。然后进入META-INF/MANIFEST.MF,用记事本打开MANIFEST.MF文件。您可以在那里找到实施版本。

Just rename the extension with .zip instead of .jar. Then go to META-INF/MANIFEST.MF and open the MANIFEST.MF file with notepad. You can find the implementation version there.

云柯 2024-11-10 23:54:24

可以使用命令java -jar jarname进行检查

It can be checked with a command java -jar jarname

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文