zoukankan      html  css  js  c++  java
  • Java中如何更换jar包中的.class文件并重新打包

     现实Java Web开发中,需要引入不少jar包,从而使用其中封装好的对象或方法。

     但是需要注意jar包的版本是否与当前jdk的版本兼容,否则将会引起项目启动异常,异常代码如下所示。

     错误代码eg: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [beans.xml]; nested exception is java.lang.IllegalStateException: Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
        at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
        at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)

    如何解决这个问题?

    一、降低JDK版本,将jdk1.8换成jdk1.7

       在本机安装jdk1.7,然后在项目的java build path中选择jdk1.7,因为该jar包版本出来的时候,jdk最新版本可能才1.7,所以选择jdk1.7的版本(尝试了这种方法,并没有效果,原因未知.......)。

    二、更换jar包中.class文件,使其兼容jdk1.8

      具体步骤:

      1.在eclipse中新建一个java类。

         内容如下:

    package org.springframework.core;
    
    public abstract class JdkVersion {
        public static final int JAVA_13 = 0;
        public static final int JAVA_14 = 1;
        public static final int JAVA_15 = 2;
        public static final int JAVA_16 = 3;
        public static final int JAVA_17 = 4;
        //for jre 1.8
        public static final int JAVA_18 = 5;
        private static final String javaVersion = System
                .getProperty("java.version");
        private static final int majorJavaVersion;
        public static String getJavaVersion() {
            return javaVersion;
        }
        public static int getMajorJavaVersion() {
            return majorJavaVersion;
        }
        public static boolean isAtLeastJava14() {
            return true;
        }
        public static boolean isAtLeastJava15() {
            return getMajorJavaVersion() >= 2;
        }
        public static boolean isAtLeastJava16() {
            return getMajorJavaVersion() >= 3;
        }
        static {
            //for jre 1.8
            if (javaVersion.indexOf("1.8.") != -1) {
                majorJavaVersion = 5;
            }else if (javaVersion.indexOf("1.7.") != -1) {
                majorJavaVersion = 4;
            } else if (javaVersion.indexOf("1.6.") != -1) {
                majorJavaVersion = 3;
            } else if (javaVersion.indexOf("1.5.") != -1) {
                majorJavaVersion = 2;
            } else {
                majorJavaVersion = 1;
            }
        }
    }

       2.生成该java类的.class文件。

         在命令提示框中定位到java类,然后输入指令:javac UserAction.java(UserAction为类名)

         

       3.修改jar包中的.class文件。

          使用解压工具打开jar包,便可以看到其中的.class文件,然后将刚刚生成的.class文件(UserAction.class),覆盖原来jar包中的UserAction.class文件。(提前做好备份哦~~)

       4.重新打包生成jar包。

         **先用压缩工具压缩文件夹,然后再修改压缩文件后缀为.jar,此方法无效!!!

         应该使用命令提示框,定位到需要打包的文件夹。

         例如,需要打包spring这个文件夹,先定位到该文件夹,然后输入指令:jar cvfm spring.jar spring\META-INF\MANIFEST.MF -C spring/ . 

         META-INF\MANIFEST.MF文件必须存在!

         

      5.覆盖原来的jar包。

         在eclipse  java build path中重新添加该jar包。

         

         已上,就已经完成了对jar包中.class文件的替换,并重新打包生成jar包,本方法亲测可用~~~

         

         此外,还需注意的是,启动tomcat时,需在Server Locations中配置tomcat安装路径,否则在浏览器中打开项目时会报404。

         

    浮躁的人容易问:我到底该学什么;----别问,学就对了; 浮躁的人容易问:JS有钱途吗;----建议你去抢银行; 浮躁的人容易说:我要中文版!我英文不行!----不行?学呀! 浮躁的人分两种:只观望而不学的人;只学而不坚持的人; 浮躁的人永远不是一个高手。
  • 相关阅读:
    leetcode 13. Roman to Integer
    python 判断是否为有效域名
    leetcode 169. Majority Element
    leetcode 733. Flood Fill
    最大信息系数——检测变量之间非线性相关性
    leetcode 453. Minimum Moves to Equal Array Elements
    leetcode 492. Construct the Rectangle
    leetcode 598. Range Addition II
    leetcode 349. Intersection of Two Arrays
    leetcode 171. Excel Sheet Column Number
  • 原文地址:https://www.cnblogs.com/qianlang/p/10833620.html
Copyright © 2011-2022 走看看