如何以编程方式确定当前类的Java字节码版本?
我遇到的情况是,部署平台是 Java 5,开发是在 Java 6 下使用 Eclipse 进行的,我们建立了一个在开始处理给定项目时创建新工作区的过程。因此,所需步骤之一是将编译器级别设置为 Java 5,这一点经常被遗忘。
我们有一台运行部署平台的测试机,可以在其中运行我们构建的代码并在 PC 上进行初始测试,但如果我们忘记切换编译器级别,程序将无法运行。我们有一个构建服务器,用于创建发送给客户的内容,效果很好,但这是用于不需要构建服务器的开发,并且会增加不必要的等待。
问题是:CAN 我以编程方式确定当前类的字节代码版本,这样我的代码就可以在本地 PC 上测试时打印出警告了吗?
编辑:请注意,要求是针对当前班级的。这可以通过类加载器获得吗?或者我必须找到当前类的类文件,然后进行调查?
I have a situation where the deployment platform is Java 5 and the development happens with Eclipse under Java 6 where we have established a procedure of having a new workspace created when beginning work on a given project. One of the required steps is therefore setting the compiler level to Java 5, which is frequently forgotten.
We have a test machine running the deployment platform where we can run the code we build and do initial testing on our PC's, but if we forget to switch the compiler level the program cannot run. We have a build server for creating what goes to the customer, which works well, but this is for development where the build server is not needed and would add unnecessary waits.
The question is: CAN I programmatically determine the byte code version of the current class, so my code can print out a warning already while testing on my local PC?
EDIT: Please note the requirement was for the current class. Is this available through the classloadeer? Or must I locate the class file for the current class, and then investigate that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
找到在类上运行javap的简单方法
有关更多详细信息,请转到http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html
示例:
并查找以下行
Easy way to find this to run javap on class
For more details goto http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html
Example:
and look for following lines
您可以将类文件加载为 资源 和 解析前八个字节。
You could load the class file as a resource and parse the first eight bytes.
看看问题: Java API 可以找出编译类文件的 JDK 版本?
Take a look at question: Java API to find out the JDK version a class file is compiled for?
这是 Java 类文件格式描述符:
Java 类文件格式
这里是主要的版本值:
public static final int JDK14_MAJOR_VERSION = 48;
公共静态最终int JDK15_MAJOR_VERSION = 49;
公共静态最终int JDK16_MAJOR_VERSION = 50;
现在,读取包含 Java 代码的类文件并检查主要版本以了解哪个 JVM 生成了它
here is the Java Class File Format descriptor:
Java Class File Format
and here the major version values:
public static final int JDK14_MAJOR_VERSION = 48;
public static final int JDK15_MAJOR_VERSION = 49;
public static final int JDK16_MAJOR_VERSION = 50;
Now, read the class file with Java code and check the major version to know which JVM generated it
类文件内容的字节 5 到 8 是十六进制版本号。您可以使用 Java 代码(或任何其他语言)来解析版本号。
Bytes 5 through 8 of a class file content is the version number in hex. You can use Java code (or any other language) to parse the version number.