当前位置:网站首页>Jar批量管理小工具
Jar批量管理小工具
2022-07-04 23:01:00 【lijiamin-】
前言
一个Jar包管理的小工具,目前有如下几个作用,后面可以慢慢扩展
1.搜索当前项目路径下的所有jar并复制到某一路径下
2.执行某个目录下的jar包批量运行启动
3.Kill所有java进程
一、代码结构
这些功能后面可以自行慢慢扩展,我目前只做到了我需要的那部分,首先看看文件处理
JarManagerApplication
package com.ljm;
import com.ljm.play.One;
import com.ljm.play.Two;
import java.io.IOException;
import java.util.Scanner;
/** * 一个JAR管理小脚本 * 1.搜索当前项目路径下的所有jar并复制到某一路径下 * 2.执行某个目录下的jar包批量运行启动 * 3.kill所有java进程 * @author 李家民 */
public class JarManagerApplication {
public static void main(String[] args) {
menu();
String InputStr = null;
// 创建一个扫描器对象 用于接收键盘数据
Scanner scannerShow = new Scanner(System.in);
while (!scannerShow.hasNext() == true) {
// 等待用户输入
}
InputStr = scannerShow.next();
System.out.println("您输入的功能项是:" + InputStr);
switch (InputStr) {
case "1":
// 1.搜索当前项目路径下的所有jar并复制到某一路径下
One.selectMethod();
One.copyMethod();
break;
case "2":
// 2.执行某个目录下的jar包批量运行启动
Two.runJar();
break;
case "3":
// 3.kill所有java进程
break;
default:
System.out.println("===参数错误===");
break;
}
// 程序退出
System.exit(0);
}
/** * 一个JAR管理小脚本 * 1.搜索当前项目路径下的所有jar并复制到某一路径下 * 2.执行某个目录下的jar包批量运行启动 * 3.kill所有java进程 */
public static void menu() {
System.out.println("一个JAR管理小脚本-----");
System.out.println("请输入功能项(数字):");
System.out.println("1.搜索当前项目路径下的所有jar并复制到某一路径下");
System.out.println("2.执行某个目录下的jar包批量运行启动");
System.out.println("3.kill所有java进程");
System.out.println();
}
}
One
package com.ljm.play;
import com.ljm.handle.FileHandler;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
public class One {
/** * 查询搜索 */
public static void selectMethod() {
// E:\44_JAVA_Protect\杭州地铁\hangzhou-metro-server-new
String pathInputStr = "";
// 创建一个扫描器对象 用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入项目路径:");
// 判断用户有没有输入字符串
if (scanner.hasNext() == true) {
pathInputStr = scanner.next();
System.out.println("您输入的路径是:" + pathInputStr);
}
// 搜索
FileHandler.getBladeJar(pathInputStr);
}
/** * 复制到某个路径 */
public static void copyMethod() {
// 目录位置
String pathInputStr = "";
// 创建一个扫描器对象 用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入要复制的路径:");
// 判断用户有没有输入字符串
if (scanner.hasNext() == true) {
pathInputStr = scanner.next();
System.out.println("您输入的路径是:" + pathInputStr);
}
List<String> bladeJarPathList = FileHandler.getBladeJarPathList();
List<String> bladeJarNameList = FileHandler.getBladeJarNameList();
System.out.println("开始复制.......");
for (int i = 0; i < bladeJarPathList.size(); i++) {
String sPath = bladeJarPathList.get(i);
String sName = pathInputStr + "\\" + bladeJarNameList.get(i);
try {
FileHandler.copy(sPath, sName);
System.out.println("已复制文件:" + (i + 1) + " 总数量:" + bladeJarPathList.size());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
FileHandler
package com.ljm.handle;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/** * 文件处理器 * @author 李家民 */
public class FileHandler {
private static List<String> jarPathList = new ArrayList<>();
private static List<String> jarNameList = new ArrayList<>();
/** JAR包路径 */
public static List<String> getBladeJarPathList() {
return jarPathList;
}
/** JAR包名称 */
public static List<String> getBladeJarNameList() {
return jarNameList;
}
/** * 查找所有BLADE-JAR后缀的文件 * @param path */
public static void getBladeJar(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null != files) {
for (File file2 : files) {
if (file2.isDirectory()) {
getBladeJar(file2.getAbsolutePath());
} else {
String file2AbsolutePath = file2.getAbsolutePath();
int length = file2AbsolutePath.length();
int lastIndexOf = file2AbsolutePath.lastIndexOf(".");
if (lastIndexOf <= 0) {
continue;
}
if (file2AbsolutePath.substring(lastIndexOf, length).equals(".jar")) {
if (!file2AbsolutePath.contains("common") && !file2AbsolutePath.contains("api") && !file2AbsolutePath.contains("lib")) {
jarPathList.add(file2AbsolutePath);
jarNameList.add(file2.getName());
}
}
}
}
}
}
}
/** * 从srcPathName复制文件内容到destPathName文件中 * @param srcPathName 输入文件 * @param destPathName 输出文件 * @throws IOException */
public static void copy(String srcPathName, String destPathName) throws IOException {
// 创建IO流
// FileInputStream和FileOutputStream
FileInputStream fis = new FileInputStream(srcPathName);
FileOutputStream fos = new FileOutputStream(destPathName);
// 从fis流中读取数据,写到fos流中
byte[] data = new byte[10];
int len;
while ((len = fis.read(data)) != -1) {
fos.write(data, 0, len);
}
// 关闭
fis.close();
fos.close();
}
}
上面通篇代码总结下来就几个功能点
- 搜索某个路径下的某个后缀的文件
- 复制文件
- IO流
然后看看批量执行脚本启动的代码
package com.ljm.play;
import com.ljm.handle.FileHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Two {
/** * 跑起来所有Jar */
public static void runJar() {
String pathInputStr = "";
// 创建一个扫描器对象 用于接收键盘数据
Scanner scanner = new Scanner(System.in);
System.out.println("请输入JAR路径:");
// 判断用户有没有输入字符串
if (scanner.hasNext() == true) {
pathInputStr = scanner.next();
System.out.println("您输入的路径是:" + pathInputStr);
}
// 搜索
FileHandler.getBladeJar(pathInputStr);
List<String> jarPathList = FileHandler.getBladeJarPathList();
// cmd
try {
FileWriter fileWriter = new FileWriter(pathInputStr + "\\" + "start.bat");
fileWriter.append("@echo off \n");
fileWriter.append("\n");
for (String strP : jarPathList) {
fileWriter.append("start java -d64 -server -Xms512m -Xmx2048m -jar " + strP + "\n");
fileWriter.append("choice /t 10 /d y /n > null " + "\n");
fileWriter.append("\n");
}
fileWriter.flush();
fileWriter.close();
Process p = Runtime.getRuntime().exec("cmd /c " + pathInputStr + "\\" + "start.bat");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
真香,缺点就是路径要全英文,这个还没解决,晚点。
二、开源仓库地址
地址 |
---|
官方还在审核,待定 |
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容。
边栏推荐
- Redis: redis transactions
- Pagoda 7.9.2 pagoda control panel bypasses mobile phone binding authentication bypasses official authentication
- Redis入门完整教程:哈希说明
- 【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
- 微信小程序显示样式知识点总结
- A complete tutorial for getting started with redis: transactions and Lua
- Basic use and upgrade of Android native database
- Wechat official account solves the cache problem of entering from the customized menu
- colResizable. JS auto adjust table width plug-in
- ffmpeg快速剪辑
猜你喜欢
D3.js+Three. JS data visualization 3D Earth JS special effect
Redis: redis transactions
A complete tutorial for getting started with redis: Pipeline
【图论】拓扑排序
PS style JS webpage graffiti board plug-in
CTF競賽題解之stm32逆向入門
Qualcomm WLAN framework learning (30) -- components supporting dual sta
Qt个人学习总结
[binary tree] the maximum difference between a node and its ancestor
Excel 快捷键-随时补充
随机推荐
A complete tutorial for getting started with redis: transactions and Lua
Redis: redis message publishing and subscription (understand)
字体设计符号组合多功能微信小程序源码
[sword finger offer] questions 1-5
How to choose a securities company? Is it safe to open an account on your mobile phone
The Chinese output of servlet server and client is garbled
[crawler] XPath for data extraction
JS 3D explosive fragment image switching JS special effect
[odx Studio Edit pdx] - 0.2 - Comment comparer deux fichiers pdx / odx
【taichi】用最少的修改将太极的pbf2d(基于位置的流体模拟)改为pbf3d
该如何去选择证券公司,手机上开户安不安全
ECS settings SSH key login
Redis入门完整教程:列表讲解
Redis入门完整教程:Redis Shell
Servlet+jdbc+mysql simple web exercise
【二叉树】节点与其祖先之间的最大差值
图片懒加载的原理
Redis introduction complete tutorial: Collection details
cout/cerr/clog的区别
Excel shortcut keys - always add