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());
 }
}

更新你的jar包的更多相关文章

  1. Maven 小技巧之 自动更新你的jar包

    在做selenium 自动化测试的时候,我们经常遇到这样的情况:浏览器悄悄升级了.紧接着所有测试用例都Fail. 检查过日志之后发现,原来是升级过的浏览器,我们用原来的selenium已经无法操作. ...

  2. 删除 maven仓库,更新失败的jar包命令

    set REPOSITORY_PATH=D:\maven_cpbsrem ص‎شعثرث÷...for /f "delims=" %%i in ('dir /b /s " ...

  3. maven依赖jar包更新,业务jar需同步更新(业务jar依赖API)

    背景: 环境出现问题,定位为依赖jar缺失,修改工程pom文件补充依赖jar. 更新要点说明: 依赖jar,更新提交 业务jar,也需更新提交:maven构建会把依赖jar引用进去,更新环境如果单独更 ...

  4. 如何更新maven需要的jar包

    第一次使用maven,检出项目生成时出现缺少xxx.jar,目录在C盘下: 拿mybatis-spring-1.2.2.jar来说,发现在C:\Users\Administrator\.m2\repo ...

  5. 贡献你的代码,将jar包发布到Maven中央仓库以及常见错误的解决办法

    前几天将自己的日志工具发布到了Maven中央仓库中.这个工具本省没有多少技术含量,因为是修改别人的源代码实现的,但是将jar发布到Maven仓库却收获颇丰,因为网上有些教程过时了,在此分享下自己发布j ...

  6. 使用Maven命令安装jar包到repo中

    项目中可能会碰到很多jar包,使用maven update不能更新,或者jar包是拷贝过来,不能编译的情况.此时就需要手动使用命令行安装. 例如Demo项目中提示缺少四个jar包,但是在repo中已经 ...

  7. 提取aar 包中的jar包,反编译再替换成新的aar

      参考了 http://blog.csdn.net/hekewangzi/article/details/44676797 针对aar包,增加一些说明 aar包本质应该是zip文件.可以用360解压 ...

  8. 使用Maven命令安装jar包到仓库中

    项目中可能会碰到很多jar包,使用maven update不能更新,或者jar包是拷贝过来,不能编译的情况.此时就需要手动使用命令行安装. 例如Demo项目中提示缺少四个jar包,但是在repo中已经 ...

  9. eclipse maven项目如何将所有的jar包复制到lib目录下?

      1.情景展示 我们知道,maven项目的jar包并不存在于项目当中,项目所需的jar包都保存在本地仓库中,如果本地仓库没有,会从配置的中央仓库下载,如果中央仓库也没有就会报错: 如上图所示,我想将 ...

随机推荐

  1. 【html】【19】高级篇--大事件时间轴

    下载: http://sc.chinaz.com/jiaoben/131112181390.htm 其它: http://sc.chinaz.com/tag_jiaoben/shijianzhou.h ...

  2. C 语言 查找一个字符串2在字符串1中出现的次数

    #include <stdio.h> #include <windows.h> int main() { ], b[]; char *temp; ; memset( a, ); ...

  3. (转)Quartz.NET管理类

    最近做项目设计到Quartz.NET,写了一个Quartz.NET管理类,在此记录下. public class QuartzManager<T> where T : class,IJob ...

  4. About 'atoi'

    在很多地方我们看到的都是:atoi 是一个 C Reference function atoi() convert a string to an integer. This function of s ...

  5. "const wchar_t is incompatible with parameter of type "LPCSTR"

    MessageBox(NULL, L"TEST", L"TEST", MB_OK); You may get this error if you "U ...

  6. linux命令行下命令参数前的一横(-)和两横(--)的区别

    原文转自:http://blog.csdn.net/songjinshi/article/details/6816776 在解释这些区别之前我们先了解一下有关linux的背景知识,这个需要大家先认真看 ...

  7. OpenJudge/Poj 1661 帮助 Jimmy

    1.链接地址: bailian.openjudge.cn/practice/1661 http://poj.org/problem?id=1661 2.题目: 总Time Limit: 1000ms ...

  8. C++学习 (转)

    1.把C++当成一门新的语言学习: 2.看<Thinking In C++>: 3.看<The C++ Programming Language>和<Inside The ...

  9. rsync+inotity

    rsync默认端口:873xinetd默认服务 inotify参数详解inotifywait-r:递归-q:只打印事件-m:始终监听事件--excludei:排除--timefmt:时间格式--for ...

  10. android的liveview装载数据

    设置布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro ...