gpt4 book ai didi

java - 我怎么知道 .class 文件是用哪个 Java 版本编译的?

转载 作者:搜寻专家 更新时间:2023-11-01 02:16:18 24 4
gpt4 key购买 nike

我试图找出如何通过查看二进制数据来确定 .class 文件的版本。我知道使用 javap 会告诉我版本 - 但不幸的是,我并不总是在我需要检查的机器上安装了 JDK。但是我确实可以使用普通的旧 HEX 编辑器 ...

我试着从经过认证的来源找到关于这个的文档——我所能想到的就是这个:http://fredericktang.wordpress.com/2007/09/28/java-class-file-version/
尽管这篇博文写得非常好 - 它并没有得到官方 Java 文档的真正支持。

有什么提示吗?

编辑:没关系 - 抱歉打错了 - 我找到了规范: here

最佳答案

您引用的文档实际上是 JVM 类文件规范的逐字转录(更简单的英语)。

我手边的一些历史值

Major  Major(hex) Minor Java platform version 
45 2d 3 1.0
45 2d 3 1.1
46 2e 0 1.2
47 2f 0 1.3
48 30 0 1.4
49 31 0 1.5
50 32 0 1.6

--- 根据 Neil 的观察进行编辑,即 1.0 和 1.1 使用相同的 Major Minor 数字 ---

从类头中,无法确定该类是针对 Java 1.0 还是 1.1 版本编译的。也就是说,还有许多其他技术可以帮助确定一个类是针对 1.0 还是 1.1 编译的。

首先,如果该类是针对 1.1 编译的,它可能包含一个仅限 1.1 的库类。一旦您在其中一个类中找到其中一个,您就可以放心地断定它是针对 Java 1.1 编译的类。

请注意,可以在类的开头搜索常量池以确定类文件需要哪些类来完全解析其所有外部引用。

单独搜索类可能很麻烦,幸运的是也添加了很多包。以下包是 Java 1.1 特有的,即它们不包含在 Java 1.0 中

+ java.awt.datatransfer
+ java.awt.event
+ java.beans
+ java.lang.reflect
+ java.math
+ java.rmi.dgc
+ java.rmi
+ java.rmi.registry
+ java.rmi.server
+ java.security.acl
+ java.security
+ java.security.interfaces
+ java.sql
+ java.text
+ java.util.zip

另一方面,对以下方法的大量调用表明(但不是实际证据)该代码是针对 Java 1.0 编写的,因为由于新的 awt 事件重组,它们在 Java 1.1 中已被弃用。

Deprecated Classes

java.io.LineNumberInputStream:
LineNumberInputStream is deprecated.
java.io.StringBufferInputStream:
StringBufferInputStream is deprecated.

Deprecated Methods

java.awt.BorderLayout:
addLayoutComponent() is deprecated.
java.awt.CardLayout:
addLayoutComponent() is deprecated.
java.awt.CheckboxGroup:
getCurrent() is deprecated.
java.awt.CheckboxGroup:
setCurrent() is deprecated.
java.awt.Choice:
countItems() is deprecated.
java.awt.Component:
getPeer() is deprecated.
java.awt.Component:
enable() is deprecated.
java.awt.Component:
enable() is deprecated.
java.awt.Component:
disable() is deprecated.
java.awt.Component:
show() is deprecated.
java.awt.Component:
show() is deprecated.
java.awt.Component:
hide() is deprecated.
java.awt.Component:
location() is deprecated.
java.awt.Component:
move() is deprecated.
java.awt.Component:
size() is deprecated.
java.awt.Component:
resize() is deprecated.
java.awt.Component:
resize() is deprecated.
java.awt.Component:
bounds() is deprecated.
java.awt.Component:
reshape() is deprecated.
java.awt.Component:
preferredSize() is deprecated.
java.awt.Component:
minimumSize() is deprecated.
java.awt.Component:
layout() is deprecated.
java.awt.Component:
inside() is deprecated.
java.awt.Component:
locate() is deprecated.
java.awt.Component:
deliverEvent() is deprecated.
java.awt.Component:
postEvent() is deprecated.
java.awt.Component:
handleEvent() is deprecated.
java.awt.Component:
mouseDown() is deprecated.
java.awt.Component:
mouseDrag() is deprecated.
java.awt.Component:
mouseUp() is deprecated.
java.awt.Component:
mouseMove() is deprecated.
java.awt.Component:
mouseEnter() is deprecated.
java.awt.Component:
mouseExit() is deprecated.
java.awt.Component:
keyDown() is deprecated.
java.awt.Component:
keyUp() is deprecated.
java.awt.Component:
action() is deprecated.
java.awt.Component:
gotFocus() is deprecated.
java.awt.Component:
lostFocus() is deprecated.
java.awt.Component:
nextFocus() is deprecated.
java.awt.Container:
countComponents() is deprecated.
java.awt.Container:
insets() is deprecated.
java.awt.Container:
layout() is deprecated.
java.awt.Container:
preferredSize() is deprecated.
java.awt.Container:
minimumSize() is deprecated.
java.awt.Container:
deliverEvent() is deprecated.
java.awt.Container:
locate() is deprecated.
java.awt.FontMetrics:
getMaxDecent() is deprecated.
java.awt.Frame:
setCursor() is deprecated.
java.awt.Frame:
getCursorType() is deprecated.
java.awt.Graphics:
getClipRect() is deprecated.
java.awt.List:
countItems() is deprecated.
java.awt.List:
clear() is deprecated.
java.awt.List:
isSelected() is deprecated.
java.awt.List:
allowsMultipleSelections() is deprecated.
java.awt.List:
setMultipleSelections() is deprecated.
java.awt.List:
preferredSize() is deprecated.
java.awt.List:
preferredSize() is deprecated.
java.awt.List:
minimumSize() is deprecated.
java.awt.List:
minimumSize() is deprecated.
java.awt.List:
delItems() is deprecated.
java.awt.Menu:
countItems() is deprecated.
java.awt.MenuBar:
countMenus() is deprecated.
java.awt.MenuComponent:
getPeer() is deprecated.
java.awt.MenuComponent:
postEvent() is deprecated.
java.awt.MenuContainer:
postEvent() is deprecated.
java.awt.MenuItem:
enable() is deprecated.
java.awt.MenuItem:
enable() is deprecated.
java.awt.MenuItem:
disable() is deprecated.
java.awt.Polygon:
getBoundingBox() is deprecated.
java.awt.Polygon:
inside() is deprecated.
java.awt.Rectangle:
reshape() is deprecated.
java.awt.Rectangle:
move() is deprecated.
java.awt.Rectangle:
resize() is deprecated.
java.awt.Rectangle:
inside() is deprecated.
java.awt.ScrollPane:
layout() is deprecated.
java.awt.Scrollbar:
getVisible() is deprecated.
java.awt.Scrollbar:
setLineIncrement() is deprecated.
java.awt.Scrollbar:
getLineIncrement() is deprecated.
java.awt.Scrollbar:
setPageIncrement() is deprecated.
java.awt.Scrollbar:
getPageIncrement() is deprecated.
java.awt.TextArea:
insertText() is deprecated.
java.awt.TextArea:
appendText() is deprecated.
java.awt.TextArea:
replaceText() is deprecated.
java.awt.TextArea:
preferredSize() is deprecated.
java.awt.TextArea:
preferredSize() is deprecated.
java.awt.TextArea:
minimumSize() is deprecated.
java.awt.TextArea:
minimumSize() is deprecated.
java.awt.TextField:
setEchoCharacter() is deprecated.
java.awt.TextField:
preferredSize() is deprecated.
java.awt.TextField:
preferredSize() is deprecated.
java.awt.TextField:
minimumSize() is deprecated.
java.awt.TextField:
minimumSize() is deprecated.
java.awt.Window:
postEvent() is deprecated.
java.io.ByteArrayOutputStream:
toString() is deprecated.
java.io.DataInputStream:
readLine() is deprecated.
java.io.PrintStream:
PrintStream() is deprecated.
java.io.PrintStream:
PrintStream() is deprecated.
java.io.StreamTokenizer:
StreamTokenizer() is deprecated.
java.lang.Character:
isJavaLetter() is deprecated.
java.lang.Character:
isJavaLetterOrDigit() is deprecated.
java.lang.Character:
isSpace() is deprecated.
java.lang.ClassLoader:
defineClass() is deprecated.
java.lang.Runtime:
getLocalizedInputStream() is deprecated.
java.lang.Runtime:
getLocalizedOutputStream() is deprecated.
java.lang.String:
String() is deprecated.
java.lang.String:
String() is deprecated.
java.lang.String:
getBytes() is deprecated.
java.lang.System:
getenv() is deprecated.
java.net.Socket:
Socket() is deprecated.
java.net.Socket:
Socket() is deprecated.
java.util.Date:
Date() is deprecated.
java.util.Date:
Date() is deprecated.
java.util.Date:
Date() is deprecated.
java.util.Date:
Date() is deprecated.
java.util.Date:
UTC() is deprecated.
java.util.Date:
parse() is deprecated.
java.util.Date:
getYear() is deprecated.
java.util.Date:
setYear() is deprecated.
java.util.Date:
getMonth() is deprecated.
java.util.Date:
setMonth() is deprecated.
java.util.Date:
getDate() is deprecated.
java.util.Date:
setDate() is deprecated.
java.util.Date:
getDay() is deprecated.
java.util.Date:
getHours() is deprecated.
java.util.Date:
setHours() is deprecated.
java.util.Date:
getMinutes() is deprecated.
java.util.Date:
setMinutes() is deprecated.
java.util.Date:
getSeconds() is deprecated.
java.util.Date:
setSeconds() is deprecated.
java.util.Date:
toLocaleString() is deprecated.
java.util.Date:
toGMTString() is deprecated.
java.util.Date:
getTimezoneOffset() is deprecated.

可以证明 Java 代码是 1.1 版,但仅在某些情况下(如果代码使用新添加的 1.1 功能)。通常不可能证明 Java 代码是 1.0,但没有使用任何 Java 1.1 功能,并且大量使用 Java 已弃用的功能暗示了针对 Java 1.0 环境的类。

我不知道有任何类内标记可能能够区分这两者(行号表中的差异等),主要是因为我不需要在十六进制中挖掘 1.0 和 1.1 类编辑。

关于java - 我怎么知道 .class 文件是用哪个 Java 版本编译的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5782107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com