• 识别jar的编译JDK版本


    解压jar,获取xxx.calss文件

    dos命令行javap -verbose classname

    import java.io.InputStream;
    import java.io.PrintWriter;
    
    public class CMD命令 {
        public static void main(String[] args) {
            String filePath = "D:/git/new_baiwang/sdk-java/sdk-java/target/classes/com/baiwang/bop/Constants.class";
            docmd(filePath);
        }
    
        static void docmd(String filePath) {
            String[] command = { "cmd", };
            Process p = null;
            try {
                p = Runtime.getRuntime().exec(command);
                new Thread(new SyncPipe(p.getInputStream())).start();
                new Thread(new SyncPipe(p.getErrorStream())).start();
                PrintWriter stdin = new PrintWriter(p.getOutputStream());
                String ml = "javap -verbose " + filePath;
                stdin.println(ml);
                stdin.close();
            } catch (Exception e) {
                throw new RuntimeException("编译出现错误:" + e.getMessage());
            }
        }
    }
    
    class SyncPipe implements Runnable {
    
        private final InputStream istrm_;
    
        public SyncPipe(InputStream istrm) {
            istrm_ = istrm;
        }
    
        public void run() {
            try {
                final byte[] buffer = new byte[10240];
                StringBuffer sbf = new StringBuffer();
                for (int length = 0; (length = istrm_.read(buffer)) != -1;) {
                    sbf.append(new String(buffer, 0, length));
                }
                String msg = sbf.toString();
                if (msg.length() > 10) {
                    if (msg.contains("major version: 50")) {
                        System.out.println("编译的JDK版本是1.6");
                    } else if (msg.contains("major version: 51")) {
                        System.out.println("编译的JDK版本是1.7");
                    } else if (msg.contains("major version: 52")) {
                        System.out.println("编译的JDK版本是1.8");
                    } else {
                        System.err.println("运行错误,未知版本!");
                        System.out.println(msg);
                    }
    
                } else {
                    System.err.println("运行错误,没有想要的信息!");
                }
            } catch (Exception e) {
                throw new RuntimeException("处理命令出现错误:" + e.getMessage());
            }
        }
    }
  • 相关阅读:
    URL Rewrite
    Visual Studio调试Asp程序
    IE6下雪碧图多次请求fix方法
    使用antixss防御xss
    字典及其增删改查,解构用法
    逻辑运算,编码问题
    字符串数据类型及其用法
    数据类型,if 语句,while语句
    列表和字典循环遍历时的删除问题,集合
    列表,及其增删改查,元组
  • 原文地址:https://www.cnblogs.com/chenglc/p/7517706.html
Copyright © 2020-2023  润新知