简体   繁体   English

如何判断编译jar的java版本范围

[英]How to tell the range of java versions a jar is compiled for

How to see which java versions a compiled jar file will work with? 如何查看已编译的jar文件将使用哪些Java版本?

Thanks 谢谢

generally MANIFEST.MF file has this information as an attribute, if you don't find it, extract the jar and choose a class and do 通常MANIFEST.MF文件将此信息作为属性,如果找不到,请解压缩jar并选择一个类并执行

$javap -verbose SomeClass.class  | grep 'major'
  major version: 50

and map the javac version from that major version 并映射该主要版本的javac版本

Here is the structure of a compiled java class file stated from this link : 这是通过此链接说明的已编译Java类文件的结构:

http://en.wikipedia.org/wiki/Java_class_file http://en.wikipedia.org/wiki/Java_class_file

Sections[edit] There are 10 basic sections to the Java Class File structure: 章节[编辑] Java类文件结构有10个基本部分:

  • Magic Number: 0xCAFEBABE 幻数:0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file 类文件格式的版本:类文件的次要和主要版本
  • Constant Pool: Pool of constants for the class 常量池:类的常量池
  • Access Flags: for example whether the class is abstract, static, etc. 访问标志:例如,类是抽象的,静态的等等。
  • This Class: The name of the current class 此类:当前类的名称
  • Super Class: The name of the super class 超级班级:超级班级的名称
  • Interfaces: Any interfaces in the class 接口:类中的任何接口
  • Fields: Any fields in the class 栏位:课程中的所有栏位
  • Methods: Any methods in the class 方法:类中的任何方法
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.) 属性:类的任何属性(例如源文件的名称等)

As you can see, the second point is the version. 如您所见,第二点是版本。 Therefore, download an hex editor, open any .class file located in the jar and you will be able to read the version. 因此,下载一个十六进制编辑器,打开jar中的任何.class文件,您将能够读取该版本。

Edit : Altough I never verified, the byte offset for the version is suppose to be between 4 to 7, once again from the same link. 编辑: Altough我从未验证过,版本的字节偏移量假设在4到7之间,再次来自同一个链接。

Edit 2 : If you prefer doing it with command, check this thread : how to check the jdk version used to compile a .class file 编辑2:如果您更喜欢使用命令,请检查此线程: 如何检查用于编译.class文件的jdk版本

There is no such information in the Jar file, especially when considering that your term “java versions a compiled jar file will work with” heavily depends on how you define “will work”. Jar文件中没有这样的信息,特别是考虑到你的术语“java版本编译的jar文件将与之一起使用”在很大程度上取决于你如何定义“将工作”。 An application could start, run for a second and then terminate with an exception. 应用程序可以启动,运行一秒钟,然后异常终止。 Does that already fulfill your definition of “does work”? 这是否已经满足您对“可行”的定义?

As said by others, there is a version number within the class files. 正如其他人所说,类文件中有一个版本号。 You can find the information about how to map that version number to Java version here . 您可以在此处找到有关如何将版本号映射到Java版本的信息

However, that version number only tells you the minimum JVM version that is needed to load that class file. 但是,该版本号仅告诉您加载该类文件所需的最低JVM版本。 It does not tell you which API it targets. 它不会告诉您它所针对的API。 It's perfectly legal to compile a class file compatible with a Java 1.1 JVM but using Java 8 APIs. 编译与Java 1.1 JVM兼容但使用Java 8 API的类文件是完全合法的。

You could scan all class and member references of all class files within a Jar file and compare to the official API versions, however that only tells you which is effectively used, not what's intentionally targeted. 您可以扫描Jar文件中所有类文件的所有类和成员引用,并与官方API版本进行比较,但这只会告诉您哪些是有效使用的,而不是有意使用的。 Eg the application could still rely on certain bugs being fixed or missing functionality filled into already existing APIs. 例如,应用程序仍可能依赖于已修复的某些错误或缺少已存在的API中的功能。 Eg whether an application relies on the requirement “the JRE's AWT can load PNG images with correct transparency support” can not be concluded by looking at the class file version number or at which API it refers to. 例如,应用程序是否依赖于“JRE的AWT可以加载具有正确透明度支持的PNG图像”的要求,无法通过查看类文件版本号或它引用的API来得出结论。

Specifying which Java version an application or library requires is beyond the scope of simple Jar files, eg you may have a look at OSGi or Java Webstart. 指定应用程序或库所需的Java版本超出了简单Jar文件的范围,例如,您可以查看OSGi或Java Webstart。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM