当前位置:网站首页>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);
}
}
}
真香,缺点就是路径要全英文,这个还没解决,晚点。
二、开源仓库地址
| 地址 |
|---|
| 官方还在审核,待定 |
总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容。
边栏推荐
- Ffmpeg quick clip
- Redis入门完整教程:有序集合详解
- [binary tree] the maximum difference between a node and its ancestor
- Excel 快捷键-随时补充
- The caching feature of docker image and dockerfile
- Basic knowledge of database
- MariaDB的Galera集群应用场景--数据库多主多活
- Tweenmax emoticon button JS special effect
- mamp下缺少pcntl扩展的解决办法,Fatal error: Call to undefined function pcntl_signal()
- [Taichi] change pbf2d (position based fluid simulation) of Taiji to pbf3d with minimal modification
猜你喜欢

Actual combat simulation │ JWT login authentication

Redis démarrer le tutoriel complet: Pipeline

Redis入门完整教程:初识Redis

A complete tutorial for getting started with redis: getting to know redis for the first time

壁仞科技研究院前沿技术文章精选

QT drawing network topology diagram (connecting database, recursive function, infinite drawing, dragging nodes)

MariaDB的Galera集群应用场景--数据库多主多活

Redis:Redis消息的发布与订阅(了解)

实战模拟│JWT 登录认证

A complete tutorial for getting started with redis: redis shell
随机推荐
How to choose a securities company? Is it safe to open an account on your mobile phone
Object detection based on OpenCV haarcascades
时间 (计算)总工具类 例子: 今年开始时间和今年结束时间等
phpcms付费阅读功能支付宝支付
CTF競賽題解之stm32逆向入門
ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
Redis入门完整教程:API的理解和使用
Explanation of bitwise operators
ScriptableObject
高通WLAN框架学习(30)-- 支持双STA的组件
MariaDB's Galera cluster application scenario -- multi master and multi active databases
P2181 diagonal and p1030 [noip2001 popularization group] arrange in order
Redis入门完整教程:Pipeline
OSEK标准ISO_17356汇总介绍
Excel shortcut keys - always add
A complete tutorial for getting started with redis: hyperloglog
Header file duplicate definition problem solving "c1014 error“
A complete tutorial for getting started with redis: redis usage scenarios
Redis: redis configuration file related configuration and redis persistence
Redis入门完整教程:初识Redis