Do any of you know of a tool that will search for .class files and then display their compiled versions?
您是否知道有一个工具可以搜索.class文件,然后显示它们的编译版本?
I know you can look at them individually in a hex editor but I have a lot of class files to look over (something in my giant application is compiling to Java6 for some reason).
我知道您可以在十六进制编辑器中单独查看它们,但是我有许多类文件需要查看(出于某种原因,我的大型应用程序中的一些内容正在编译为Java6)。
7 个解决方案
#1
133
Use the javap tool that comes with the JDK. The -verbose
option will print the version number of the class file.
使用JDK附带的javap工具。-verbose选项将打印类文件的版本号。
> javap -verbose MyClass
Compiled from "MyClass.java"
public class MyClass
SourceFile: "MyClass.java"
minor version: 0
major version: 46
...
To only show the version:
只显示版本:
WINDOWS> javap -verbose MyClass | find "version"
LINUX > javap -verbose MyClass | grep version
#2
40
It is easy enough to read the class file signature and get these values without a 3rd party API. All you need to do is read the first 8 bytes.
读取类文件签名并在没有第三方API的情况下获取这些值非常容易。你所需要做的就是读取前8个字节。
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
For class file version 51.0 (Java 7), the opening bytes are:
对于类文件51.0 (Java 7),打开的字节为:
CA FE BA BE 00 00 00 33
...where 0xCAFEBABE are the magic bytes, 0x0000 is the minor version and 0x0033 is the major version.
…0xCAFEBABE是神奇的字节,0x0000是次要版本,0x0033是主要版本。
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
ClassLoader loader = Demo.class.getClassLoader();
try (InputStream in = loader.getResourceAsStream("Demo.class");
DataInputStream data = new DataInputStream(in)) {
if (0xCAFEBABE != data.readInt()) {
throw new IOException("invalid header");
}
int minor = data.readUnsignedShort();
int major = data.readUnsignedShort();
System.out.println(major + "." + minor);
}
}
}
Walking directories (File) and archives (JarFile) looking for class files is trivial.
查找类文件的行走目录(文件)和归档(JarFile)是微不足道的。
Oracle's Joe Darcy's blog lists the class version to JDK version mappings up to Java 7:
Oracle的Joe Darcy的博客列出了JDK版本映射到Java 7的类版本:
Target Major.minor Hex
1.1 45.3 0x2D
1.2 46.0 0x2E
1.3 47.0 0x2F
1.4 48.0 0x30
5 (1.5) 49.0 0x31
6 (1.6) 50.0 0x32
7 (1.7) 51.0 0x33
8 (1.8) 52.0 0x34
9 53.0 0x35
#3
19
On Unix-like
在类unix
file /path/to/Thing.class
Will give the file type and version as well. Here is what the output looks like:
将提供文件类型和版本。下面是输出的样子:
compiled Java class data, version 49.0
编译Java类数据,版本49.0
#4
9
If you are on a unix system you could just do a
如果你在unix系统上,你可以做一个
find /target-folder -name \*.class | xargs file | grep "version 50\.0"
(my version of file says "compiled Java class data, version 50.0" for java6 classes).
(我的文件版本是“编译Java类数据,版本50.0”用于java6类)。
#5
5
Yet another java version check
另一个java版本检查。
od -t d -j 7 -N 1 ApplicationContextProvider.class | head -1 | awk '{print "Java", $2 - 44}'
#6
#7
2
Maybe this helps somebody, too. Looks there is more easy way to get JAVA version used to compile/build .class. This way is useful to application/class self check on JAVA version.
也许这对某些人也有帮助。看起来有更简单的方法可以让JAVA版本用于编译/构建.class。这种方法对于JAVA版本的应用程序/类自我检查非常有用。
I have gone through JDK library and found this useful constant: com.sun.deploy.config.BuiltInProperties.CURRENT_VERSION. I do not know since when it is in JAVA JDK.
我浏览了JDK库,发现了这个有用的常量:com.sun. deployment .config. build . current_version。我不知道它什么时候在JAVA JDK中。
Trying this piece of code for several version constants I get result below:
为几个版本常量尝试这段代码,我得到以下结果:
src:
src:
System.out.println("JAVA DEV ver.: " + com.sun.deploy.config.BuiltInProperties.CURRENT_VERSION);
System.out.println("JAVA RUN v. X.Y: " + System.getProperty("java.specification.version") );
System.out.println("JAVA RUN v. W.X.Y.Z: " + com.sun.deploy.config.Config.getJavaVersion() ); //_javaVersionProperty
System.out.println("JAVA RUN full ver.: " + System.getProperty("java.runtime.version") + " (may return unknown)" );
System.out.println("JAVA RUN type: " + com.sun.deploy.config.Config.getJavaRuntimeNameProperty() );
output:
输出:
JAVA DEV ver.: 1.8.0_77
JAVA RUN v. X.Y: 1.8
JAVA RUN v. W.X.Y.Z: 1.8.0_91
JAVA RUN full ver.: 1.8.0_91-b14 (may return unknown)
JAVA RUN type: Java(TM) SE Runtime Environment
In class bytecode there is really stored constant - see red marked part of Main.call - constant stored in .class bytecode
在类字节码中确实存储了常量——参见Main的红色标记部分。调用-常量存储在.class字节码中
Constant is in class used for checking if JAVA version is out of date (see How Java checks that is out of date)...
常量在类中用于检查JAVA版本是否过时(参见JAVA如何检查过期)……
#1
133
Use the javap tool that comes with the JDK. The -verbose
option will print the version number of the class file.
使用JDK附带的javap工具。-verbose选项将打印类文件的版本号。
> javap -verbose MyClass
Compiled from "MyClass.java"
public class MyClass
SourceFile: "MyClass.java"
minor version: 0
major version: 46
...
To only show the version:
只显示版本:
WINDOWS> javap -verbose MyClass | find "version"
LINUX > javap -verbose MyClass | grep version
#2
40
It is easy enough to read the class file signature and get these values without a 3rd party API. All you need to do is read the first 8 bytes.
读取类文件签名并在没有第三方API的情况下获取这些值非常容易。你所需要做的就是读取前8个字节。
ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
For class file version 51.0 (Java 7), the opening bytes are:
对于类文件51.0 (Java 7),打开的字节为:
CA FE BA BE 00 00 00 33
...where 0xCAFEBABE are the magic bytes, 0x0000 is the minor version and 0x0033 is the major version.
…0xCAFEBABE是神奇的字节,0x0000是次要版本,0x0033是主要版本。
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
ClassLoader loader = Demo.class.getClassLoader();
try (InputStream in = loader.getResourceAsStream("Demo.class");
DataInputStream data = new DataInputStream(in)) {
if (0xCAFEBABE != data.readInt()) {
throw new IOException("invalid header");
}
int minor = data.readUnsignedShort();
int major = data.readUnsignedShort();
System.out.println(major + "." + minor);
}
}
}
Walking directories (File) and archives (JarFile) looking for class files is trivial.
查找类文件的行走目录(文件)和归档(JarFile)是微不足道的。
Oracle's Joe Darcy's blog lists the class version to JDK version mappings up to Java 7:
Oracle的Joe Darcy的博客列出了JDK版本映射到Java 7的类版本:
Target Major.minor Hex
1.1 45.3 0x2D
1.2 46.0 0x2E
1.3 47.0 0x2F
1.4 48.0 0x30
5 (1.5) 49.0 0x31
6 (1.6) 50.0 0x32
7 (1.7) 51.0 0x33
8 (1.8) 52.0 0x34
9 53.0 0x35
#3
19
On Unix-like
在类unix
file /path/to/Thing.class
Will give the file type and version as well. Here is what the output looks like:
将提供文件类型和版本。下面是输出的样子:
compiled Java class data, version 49.0
编译Java类数据,版本49.0
#4
9
If you are on a unix system you could just do a
如果你在unix系统上,你可以做一个
find /target-folder -name \*.class | xargs file | grep "version 50\.0"
(my version of file says "compiled Java class data, version 50.0" for java6 classes).
(我的文件版本是“编译Java类数据,版本50.0”用于java6类)。
#5
5
Yet another java version check
另一个java版本检查。
od -t d -j 7 -N 1 ApplicationContextProvider.class | head -1 | awk '{print "Java", $2 - 44}'
#6
4
In eclipse if you don't have sources attached. Mind the first line after the attach source button.
在eclipse中,如果没有附加源代码。注意附加源按钮后的第一行。
// Compiled from CDestinoLog.java (version 1.5 : 49.0, super bit)
/ /从CDestinoLog编译。java(版本1.5:49.0,超级位)
#7
2
Maybe this helps somebody, too. Looks there is more easy way to get JAVA version used to compile/build .class. This way is useful to application/class self check on JAVA version.
也许这对某些人也有帮助。看起来有更简单的方法可以让JAVA版本用于编译/构建.class。这种方法对于JAVA版本的应用程序/类自我检查非常有用。
I have gone through JDK library and found this useful constant: com.sun.deploy.config.BuiltInProperties.CURRENT_VERSION. I do not know since when it is in JAVA JDK.
我浏览了JDK库,发现了这个有用的常量:com.sun. deployment .config. build . current_version。我不知道它什么时候在JAVA JDK中。
Trying this piece of code for several version constants I get result below:
为几个版本常量尝试这段代码,我得到以下结果:
src:
src:
System.out.println("JAVA DEV ver.: " + com.sun.deploy.config.BuiltInProperties.CURRENT_VERSION);
System.out.println("JAVA RUN v. X.Y: " + System.getProperty("java.specification.version") );
System.out.println("JAVA RUN v. W.X.Y.Z: " + com.sun.deploy.config.Config.getJavaVersion() ); //_javaVersionProperty
System.out.println("JAVA RUN full ver.: " + System.getProperty("java.runtime.version") + " (may return unknown)" );
System.out.println("JAVA RUN type: " + com.sun.deploy.config.Config.getJavaRuntimeNameProperty() );
output:
输出:
JAVA DEV ver.: 1.8.0_77
JAVA RUN v. X.Y: 1.8
JAVA RUN v. W.X.Y.Z: 1.8.0_91
JAVA RUN full ver.: 1.8.0_91-b14 (may return unknown)
JAVA RUN type: Java(TM) SE Runtime Environment
In class bytecode there is really stored constant - see red marked part of Main.call - constant stored in .class bytecode
在类字节码中确实存储了常量——参见Main的红色标记部分。调用-常量存储在.class字节码中
Constant is in class used for checking if JAVA version is out of date (see How Java checks that is out of date)...
常量在类中用于检查JAVA版本是否过时(参见JAVA如何检查过期)……