Java - 哪个版本也构建了 .jar 文件?

发布于 2024-10-07 17:07:17 字数 35 浏览 0 评论 0原文

有没有办法找出用于构建 .jar 文件的 JDK 版本?

Is there a way to find out the JDK version used to build a .jar file?

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

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

发布评论

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

评论(4

七禾 2024-10-14 17:07:17

这不可能可靠地实现,因为您甚至不需要 JDK 来构建 JAR 文件 - 它只是一个包含类和清单文件的 ZIP 文件。

您可以找到的是类文件的版本使用格式。主要版本号映射到主要JDK版本:

J2SE 6.0 = 50 (0x32 hex)
J2SE 5.0 = 49 (0x31 hex)
JDK 1.4 = 48 (0x30 hex)
JDK 1.3 = 47 (0x2F hex)
JDK 1.2 = 46 (0x2E hex)
JDK 1.1 = 45 (0x2D hex)

但是,java编译器可以选择使用先前版本的类文件格式,这经常用于实现向下兼容性。但是,如果您发现类文件版本主编号为 50,您就知道这些类肯定不是使用 Java 5 或更早版本的 JDK 编译的。

That's not possible reliably, since you don't even need a JDK to build a JAR file - it's just a ZIP file with the classes and a manifest file inside.

What you can find out is what version of the class file format is used. The major version number maps to majorJDK releases:

J2SE 6.0 = 50 (0x32 hex)
J2SE 5.0 = 49 (0x31 hex)
JDK 1.4 = 48 (0x30 hex)
JDK 1.3 = 47 (0x2F hex)
JDK 1.2 = 46 (0x2E hex)
JDK 1.1 = 45 (0x2D hex)

However, the java compiler has an option to use the class file format of a previous version, which is frequently used to achieve downwards compatibility. But if you find a class file version major number of 50, you know that the classes were definitely not compiled with a Java 5 or earlier JDK.

瑾夏年华 2024-10-14 17:07:17

大多数 jar 是使用 jdk 打包提供的“jar”工具构建的。

在 SUN 的 JDK 中,提供的“jar”工具将在嵌入的 META-INF/MANIFEST.MF 文件中添加“Created-By”行。

该行将指定创建 JAR 的 JDK 版本(在我的例子中为“Created-By: 1.6.0_17 (Sun Microsystems Inc.)”)

其他 Java 供应商也倾向于这样做。

Most jars are built using the provided "jar" tool packaged with the jdk.

In SUN's JDK the provided "jar" tool will add a "Created-By" line in the embedded META-INF/MANIFEST.MF file.

That line will specify the version of the JDK that created the JAR (in my case "Created-By: 1.6.0_17 (Sun Microsystems Inc.)")

Other Java vendors tend to do the same.

楠木可依 2024-10-14 17:07:17

您可以从 jar 文件中提取类文件并使用以下命令:

javap -verbose SomeClass | grep major

应该为您提供类文件的版本号。

You can extract the class files from the jar file and use the following command:

javap -verbose SomeClass | grep major

Should give you the version number of the class files.

冷默言语 2024-10-14 17:07:17

在 Jars MANIFEST.MF 中可能有一个 Build-Jdk 属性,这应该就是您正在寻找的。

In the Jars MANIFEST.MF there may be a Build-Jdk property which should be what you're looking for.

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