java获取版本号及字节码编译版本方法示例
作者:蒋固金 时间:2023-06-22 08:44:31
前言
之所以会有这篇文章,是因为公司的开发环境比较老,寻找一些jar包的时候总是会纠结对应的编译版本,感觉很麻烦,所以写了一个工具类用于读取class或jar文件的编译版本,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
示例代码
package com.jinggujin.util;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
/**
* Java 版本工具
*
* @author jianggujin
*
*/
public class JavaVersionUtils
{
private static final int JAVA_CLASS_MAGIC = 0xCAFEBABE;
public final static int JDK_1_2 = 46;
public final static int JDK_1_3 = 47;
public final static int JDK_1_4 = 48;
public final static int JDK_5 = 49;
public final static int JDK_6 = 50;
public final static int JDK_7 = 51;
public final static int JDK_8 = 52;
/**
* 获得当前环境JDK版本
*
* @return
*/
public static int getJDKVersion()
{
String version = System.getProperty("java.version");
if (version != null && version.matches("1\\.\\d.*"))
{
int v = Integer.parseInt(version.charAt(2) + "");
if (v >= 2)
{
return 44 + v;
}
}
return -1;
}
/**
* 获得class或jar编译版本
*
* @param file
* @return
* @throws Exception
*/
public static int getCompileVersion(File file) throws Exception
{
if (file == null || !file.isFile() || !file.getName().matches(".*\\.((jar)|(class))"))
{
throw new IllegalArgumentException("the file must be a jar or class.");
}
int version = -1;
if (file.getName().endsWith("jar"))
{
JarFile jarFile = new JarFile(file);
Enumeration<JarEntry> enumeration = jarFile.entries();
while (enumeration.hasMoreElements())
{
JarEntry entry = enumeration.nextElement();
if (entry.getName().endsWith(".class"))
{
InputStream in = jarFile.getInputStream(entry);
version = getVersion(in);
in.close();
break;
}
}
jarFile.close();
}
else
{
InputStream in = new FileInputStream(file);
version = getVersion(in);
in.close();
}
return version;
}
private static int getVersion(InputStream in) throws Exception
{
DataInputStream dis = new DataInputStream(in);
// ,前面8个字节CA FE BA BE 是固定的,之后4个字节是次版本号,次版本号后面的4个字节是jdk的版本号
int magic = dis.readInt();
if (magic == JAVA_CLASS_MAGIC)
{
// int minorVersion =
dis.readUnsignedShort();
int majorVersion = dis.readUnsignedShort();
// Java 1.2 >> 46
// Java 1.3 >> 47
// Java 1.4 >> 48
// Java 5 >> 49
// Java 6 >> 50
// Java 7 >> 51
// Java 8 >> 52
return majorVersion;
}
return -1;
}
}
来源:http://blog.csdn.net/jianggujin/article/details/72899368
标签:java,版本号,字节码编译版本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android设备间实现蓝牙(Bluetooth)共享上网
2023-02-21 14:25:36
![](https://img.aspxhome.com/file/2023/1/128971_0s.jpg)
C# goto语句的具体使用
2021-07-22 22:26:22
![](https://img.aspxhome.com/file/2023/6/71766_0s.png)
java io文件操作删除文件或文件夹的7种方法
2022-08-09 13:29:21
![](https://img.aspxhome.com/file/2023/8/99848_0s.jpg)
SpringMVC Interceptor拦截器使用教程
2022-05-11 02:17:22
![](https://img.aspxhome.com/file/2023/8/67098_0s.png)
分析Android常见的内存泄露和解决方案
2023-03-24 03:06:33
![](https://img.aspxhome.com/file/2023/5/137235_0s.jpg)
Android 8.0的缓存大小和缓存清理接口方法
2023-11-09 16:47:06
SpringBoot参数校验Validator框架详解
2023-09-22 07:08:40
Android图片占用内存全面分析
2023-02-20 01:05:38
![](https://img.aspxhome.com/file/2023/6/114316_0s.jpg)
SpringCloud读取Nacos配置中心报错及遇到的坑:Could not resolve placeholder ‘xxx’ in value ‘${xxx}
2022-12-26 04:41:02
![](https://img.aspxhome.com/file/2023/5/129225_0s.png)
SpringbootJPA分页 PageRequest过时的替代方法
2022-03-10 11:53:13
![](https://img.aspxhome.com/file/2023/1/68001_0s.png)
Spring注解@DependsOn解析
2022-01-11 08:20:35
Java利用数组随机抽取幸运观众如何实现
2022-03-20 11:51:02
![](https://img.aspxhome.com/file/2023/9/78249_0s.png)
C#中使用Microsoft Unity记录日志
2023-05-09 10:37:26
![](https://img.aspxhome.com/file/2023/5/106455_0s.png)
浅谈关于Android WebView上传文件的解决方案
2021-10-29 21:33:51
C#实现控制台飞行棋小游戏
2023-01-01 15:43:12
![](https://img.aspxhome.com/file/2023/6/89786_0s.jpg)
eclipse springboot工程打war包方法及再Tomcat中运行的方法
2023-04-14 09:21:40
![](https://img.aspxhome.com/file/2023/9/78349_0s.png)
Java网络通信中ServerSocket的设计优化方案
2023-06-14 08:25:53
SpringBoot项目集成日志的实现方法
2023-06-02 23:00:23
![](https://img.aspxhome.com/file/2023/0/86050_0s.png)
cascade级联关系操作案例详解
2022-04-11 08:11:09
使用flutter创建可移动的stack小部件功能
2023-06-21 12:28:25
![](https://img.aspxhome.com/file/2023/0/57150_0s.gif)