如何以编程方式确定当前类的Java字节码版本?

发布于 2024-08-10 21:11:17 字数 394 浏览 5 评论 0原文

我遇到的情况是,部署平台是 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 技术交流群。

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

发布评论

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

评论(5

痞味浪人 2024-08-17 21:11:17

找到在类上运行javap的简单方法

有关更多详细信息,请转到http://download.oracle.com/javase/1,5.0/docs/tooldocs/windows/javap.html

示例:

M:\Projects\Project-1\ant\javap -classpath M:\Projects\Project-1\build\WEB-INF\classes -verbose com.company.action.BaseAction

并查找以下行

minor version: 0
major version: 50

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:

M:\Projects\Project-1\ant\javap -classpath M:\Projects\Project-1\build\WEB-INF\classes -verbose com.company.action.BaseAction

and look for following lines

minor version: 0
major version: 50
半世蒼涼 2024-08-17 21:11:17

您可以将类文件加载为 资源解析前八个字节

//TODO: error handling, stream closing, etc.
InputStream in = getClass().getClassLoader().getResourceAsStream(
    getClass().getName().replace('.', '/') + ".class");
DataInputStream data = new DataInputStream(in);
int magic = data.readInt();
if (magic != 0xCAFEBABE) {
  throw new IOException("Invalid Java class");
}
int minor = 0xFFFF & data.readShort();
int major = 0xFFFF & data.readShort();
System.out.println(major + "." + minor);

You could load the class file as a resource and parse the first eight bytes.

//TODO: error handling, stream closing, etc.
InputStream in = getClass().getClassLoader().getResourceAsStream(
    getClass().getName().replace('.', '/') + ".class");
DataInputStream data = new DataInputStream(in);
int magic = data.readInt();
if (magic != 0xCAFEBABE) {
  throw new IOException("Invalid Java class");
}
int minor = 0xFFFF & data.readShort();
int major = 0xFFFF & data.readShort();
System.out.println(major + "." + minor);
彼岸花似海 2024-08-17 21:11:17

这是 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

鹿! 2024-08-17 21:11:17

类文件内容的字节 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.

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