• 更新你的jar包


    jar文件:/home/resin.jar
    需更新包中com/caucho/server/port/Port.class类文件

    方法1:
    jar uf resin.jar com/caucho/server/port/Port.class
    要求是必须知道jar包的结构要非常熟悉,可以使用:jar tf resin.jar来查看。

    方法2:
    解压jar xvf resin.jar->覆盖相应类文件->再打包(jar cvf resin.jar com)

    假设目录结构是maven标准结构

    复制代码代码如下:

    -src
    -target
    -test.jar(你需要更新的jar包)
    复制代码代码如下:

    package com.foo.common.base.utils.development;

    import static org.junit.Assert.*;

    import java.io.File;
    import java.io.IOException;
    import java.util.Date;
    import java.util.Properties;

    import org.apache.commons.io.FileUtils;
    import org.junit.Test;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.core.io.ClassPathResource;

    public class JarUpdater {
     public static final Logger logger = LoggerFactory
       .getLogger(JarUpdater.class);

     @Test
     /**
      * 更新com目录下的所有文件到jar的对应目录结构中去
      * 
      * 一次成功的代码更新,我们断言jar的大小是不一样的
      */
     public void updateClass() throws IOException, InterruptedException {

      ClassPathResource myPath = new ClassPathResource(
        "jarUpdaterConfig.properties");
      Properties p = new Properties();
      p.load(myPath.getInputStream());

      ClassUpdater classUpdater = new ClassUpdater().applySettings(p);

      classUpdater.compileAndCopyClass();

      String workingDirectory = p.getProperty("workingDirectory");
      String jar4UpdateName = p.getProperty("jar4UpdateName");
      // class compile path
      String updateSourcePath = workingDirectory + "target";
      // class root folder
      String updateSourceDir = "com";

      Date startDate = new Date();

      File myJar = new File(workingDirectory + jar4UpdateName);
      if (!myJar.isFile()) {
       logger.error("file with following path {} does not exist.",
         jar4UpdateName);
       return;
      }
      long oldLength = myJar.length();
      logger.info("Now ready to update jar file with name:{},size:{}",
        myJar.getName(), myJar.length());
      String myCommand = "jar uf " + workingDirectory + jar4UpdateName
        + " -C " + updateSourcePath + " " + updateSourceDir;
      logger.info("Update command【{}】", myCommand);

      Runtime.getRuntime().exec(myCommand);

      while (!FileUtils.isFileNewer(myJar, startDate)) {
       logger.info("sleep for two seconds,checking changes...");
       Thread.sleep(2000);
      }
      assertNotEquals(
        "jar may not be updated successfully,check the code please",
        oldLength, myJar.length());
      logger.info("Now finish update jar file with size:{}", myJar.length());
     }
    }

  • 相关阅读:
    Mysql推荐使用规范
    程序员应该经常浏览的技术网站
    百度,腾讯,阿里等互联网公司年终奖发多少
    JNI技术详解,让程序有飞一般的感觉
    日志:分布式系统的核心
    Spring Boot七:Spring boot集成MyBatis
    通俗理解TCP的三次握手
    JDBC添加数据
    JDBC概念
    今天是阳光明媚的一天
  • 原文地址:https://www.cnblogs.com/timssd/p/5753718.html
Copyright © 2020-2023  润新知