删除指定java进程名的java进程


解决问题 :就平时可能启动什么java进程后,但忘了关,在运行时就会报端口占用异常之类的。

实现功能:-a 参数可以将所有的java进程删除
-add:application 添加application参数到配置文件,下次删除时默认删除配置文件中配置的参数项
-rm:application 移除配置文件中的删除项
-ls 列出配置了的所有进程名

package com.xiaoyu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 自动识别删除一些java进程
 * 
 * @author ji.zhou
 *
 */
public class KillSbPid {
	
	/**
	 * 加密的密钥
	 */
	public int secretId = 123456;

	/**
	 * 内存中的java进程名字集合
	 */
	public Set<String> target = new HashSet<>();
	
	
	/**
	 * 类加载后就将资源文件解密加载,添加到target中
	 */
	{
		String path = KillSbPid.class.getResource("/").toString();
		try {
			BufferedReader in = decryptFile(path.substring(6) + "\\pid_name");
			String len = null;
			while((len = in.readLine()) != null) {
				target.add(len);
			}
			in.close();
			new File(path.substring(6) + "\\temp").delete();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 入口函数
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		KillSbPid main = new KillSbPid();
		if (args.length > 0) {
			main.scanArgs(args);
		} else {
			main.distinguishAndDel();
		}
	}

	/**
	 * 扫描参数,对指定的参数,执行特定的操作
	 * java kill -add:application
	 * 
	 * @param args
	 * @throws IOException 
	 */
	public void scanArgs(String[] args) throws IOException {
		if(args.length > 1) {
			System.out.println("暂不支持这操作……");
		}
		String temp = args[0];
		String[] split = temp.split(":");
		if (split[0].equals("-add"))
			target.add(split[1]);
		if (split[0].equals("-rm"))
			target.remove(split[1]);
		if(split[0].equals("-ls")) {
			for(Iterator<String> i = target.iterator(); i.hasNext();) {
				String next = i.next();
				System.out.println(next);
			}
		}
		if(split[0].equals("-a"))
			killAll();
		flush();
	}
	
	/**
	 * 将内存中的java进程名字刷新到文件pid_name中
	 */
	public void flush() {
		String path = KillSbPid.class.getResource("/").toString();
		encryptFile(path.substring(6) + "\\pid_name");
	}
	
	/**
	 * 清楚所有的java进程
	 * @throws IOException
	 */
	public void killAll() throws IOException {
		Runtime runtime = Runtime.getRuntime();
		Process exec = runtime.exec("cmd /c jps");
		BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
		String len = null;
		List<String> res = new ArrayList<>();
		while ((len = in.readLine()) != null) {
			String temp = len;
			String[] split = temp.split(" ");
			if (split.length == 2) {
				if (!split[1].equals("KillSbPid")) {
					String command = "cmd /c taskkill /pid " + split[0] + " /f";
					Process msg = runtime.exec(command);
					BufferedReader out = new BufferedReader(new InputStreamReader(msg.getInputStream()));
					len = null;
					while ((len = out.readLine()) != null) {
						System.out.println(len);
					}
				}
			}
		}
	}

	/**
	 * 清除加载到内存的那些java进程
	 * @throws IOException
	 */
	public void distinguishAndDel() throws IOException {
		Runtime runtime = Runtime.getRuntime();
		Process exec = runtime.exec("cmd /c jps");
		BufferedReader in = new BufferedReader(new InputStreamReader(exec.getInputStream()));
		String len = null;
		List<String> res = new ArrayList<>();
		while ((len = in.readLine()) != null) {
			String temp = len;
			String[] split = temp.split(" ");
			if (split.length == 2) {
				if (target.contains(split[1]))
					res.add(split[0]);
			}
		}
		for (int i = 0; i < res.size(); i++) {
			String command = "cmd /c taskkill /pid " + res.get(i) + " /f";
			Process msg = runtime.exec(command);
			BufferedReader out = new BufferedReader(new InputStreamReader(msg.getInputStream()));
			len = null;
			while ((len = out.readLine()) != null) {
				System.out.println(len);
			}
		}
	}
	
	/**
	 * 加密文件
	 * @param temp
	 */
	public void encryptFile(String temp) {
		try {
			File file = new File(temp);
			FileOutputStream w = new FileOutputStream(file);
			for(Iterator<String> i = target.iterator(); i.hasNext();) {
				String next = i.next() + "\n";
				byte[] bytes = next.getBytes();
				for(int j = 0; j < bytes.length; j++) {
					bytes[j] = (byte) (bytes[j] ^ secretId);
				}
				w.write(bytes);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解密文件
	 * @param path
	 * @return
	 * @throws FileNotFoundException
	 */
	public BufferedReader decryptFile(String path) throws FileNotFoundException {
		File temp = null;
		try {
			File file = new File(path);
			FileInputStream in = new FileInputStream(file);
			int data = 0;
			temp = new File(this.getClass().getResource("/").toString().substring(6) + "\\temp");
			temp.createNewFile();
			FileOutputStream w = new FileOutputStream(temp);
			while((data = in.read()) != -1) {
				w.write(data ^ secretId);
			}
			w.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new BufferedReader(new FileReader(temp));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值