java的jar是一个很不错的技术。可是现在开源的发展,使得一个项目中会用到很多很多的jar文件(我们的一个项目中,刚开始lib目录下有超过100兆的jar文件),一直怀疑有些文件是用不到的,但是又不太确定哪些是有用的,哪些是没用的。
想了想,决定还是做个小工具,一劳永逸地解决这个问题吧。
此小工具能完成如下功能:
1、将原来lib路径的所有jar备份到lib\bak目录下;
2、删除不用的jar
那怎么判断哪些是没用的呢?
这里用到了java -verbose:class,这个命令会将所有加载class的过程打印出来,如果是jar中的class, 还会指明是从哪个jar中加载的。
因此,通过在tomcat或者其他应用服务器的启动脚本中,添加-verbose:class,然后运行,即可获得加载信息。将其复制到一个log文件中,作为参数传递给JarCleaner即可。
-
- 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.InputStream;
- import java.util.HashSet;
- import java.util.Set;
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class JarCleaner {
-
- public Set<String> getNecessaryJarSet(String appLibPath , String logFile){
-
- String pattern = "\\[Loaded.*[\\/].*\\.jar\\]";
- Set<String> jarSet = new HashSet<String>();
-
- BufferedReader br = null;
- try {
- br = new BufferedReader(new FileReader(new File(logFile)));
- } catch (FileNotFoundException ee) {
- System.out.println("Can't find the log file." + ee.getMessage());
- }
-
- String aLine = "";
-
- while(true){
-
- try {
- aLine = br.readLine();
- } catch (IOException e) {
- System.out.println("End of log file.");
- }
-
- if (aLine == null) break;
-
- if (aLine.matches(pattern)){
- String[] strs = aLine.split("from");
- if (strs.length <= 0) continue;
- String jarName = strs[strs.length - 1].trim();
- if (jarName.startsWith("file")){
- jarName = jarName.substring(6);
- }
- jarName = jarName.substring(0, jarName.length() - 1);
- if (jarName.indexOf("/") > 0){
- jarName = jarName.replaceAll("[/]", "\\\\");
- }
-
- if (jarName.startsWith(appLibPath)){
- if (!jarSet.contains(jarName))
- jarSet.add(jarName);
- }
- }
- }
- return jarSet;
- }
-
- public Set<String> backupOriginalJars(String appLibPath){
- File libPath = new File(appLibPath);
- File[] jars = libPath.listFiles();
- Set<String> jarSet = new HashSet<String>();
-
- File bakPath = new File(appLibPath + "\\bak");
- if (!bakPath.exists())
- bakPath.mkdir();
-
- for (File jar : jars){
- String jarName = jar.getAbsolutePath();
- jarName.replaceAll("[/]", "\\\\");
- jarSet.add(jarName);
-
- File newJar = new File(appLibPath + "\\bak\\" + jar.getName());
- copyFile(newJar, jar);
- }
-
- return jarSet;
- }
-
- public void cleanJars(Set<String> allJars, Set<String> jarSet){
- allJars.removeAll(jarSet);
-
- for (String jarName : allJars){
- File oldJar = new File(jarName);
- oldJar.delete();
- }
-
- }
-
- private void copyFile(File targetFile, File file) {
- if (targetFile.exists()) {
- System.out.println("File:" + targetFile.getAbsolutePath()
- + " already existed, skip that file!");
- return;
- } else {
- createFile(targetFile, true);
- }
- System.out.println("copied " + file.getAbsolutePath() + " to "
- + targetFile.getAbsolutePath());
- try {
- InputStream is = new FileInputStream(file);
- FileOutputStream fos = new FileOutputStream(targetFile);
- byte[] buffer = new byte[1024];
- while (is.read(buffer) != -1) {
- fos.write(buffer);
- }
- is.close();
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- private void createFile(File file, boolean isFile) {
- if (!file.exists()) {
- if (!file.getParentFile().exists()) {
- createFile(file.getParentFile(), false);
- } else {
- if (isFile) {
- try {
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else {
- file.mkdir();
- }
- }
- }
- }
-
-
-
-
-
- public static void main(String[] args) throws Exception{
- if (args.length != 2){
- System.out.println("Usage : java JarCleaner <webapp lib path> <class verbose log file>\n" +
- "This command will backup all jar files in your specified webapp lib path,\n" +
- "and remain the necessary jars only.\n" +
- " <webapp lib path> : the full path of your web application located.\n" +
- " <class verbose file> : the full path of your log file. \n" +
- " use:java -verbose:class to get the log content.\n\n" +
- "Note: You must shut down the server before run this command.");
- System.exit(-1);
- }
- String appLibPath = args[0].trim();
- String logFile = args[1].trim();
-
- JarCleaner cleaner = new JarCleaner();
-
- Set<String> jarSet = cleaner.getNecessaryJarSet(appLibPath, logFile);
-
-
- Set<String> allJars = cleaner.backupOriginalJars(appLibPath);
-
-
- cleaner.cleanJars(allJars, jarSet);
- }
-
- }
-
分享到:
相关推荐
Kotti 是一个基于 Pyramid 框架的 Python 内容管理系统(CMS),适合用来搭建中小型网站、文档库、企业展示平台、知识库等需要灵活内容结构和权限模型的项目。它本身更像一个可以二次开发的 CMS 框架,比 WordPress、Drupal 这类“一装就用”的系统更倾向于开发者定制和扩展。 这是支持pyramid2.x版本的kotti! tar -xzvf kotti1.0.tar.gz 解压缩 进入目录执行 pip install -e . 来安装, 然后执行pserve app.ini 启动。 用浏览器浏览127.0.0.1:5000 即可浏览。 用户名admin ,口令qwerty
cmd-bat-批处理-脚本-hello world.zip
知识付费系统自动采集V3.0 跳转不卡顿+搭建教程,不和外面的一样跳转卡顿,这个跳转不卡顿,支持三级分销。
在Matlab环境下,对图像进行特征提取时,主要涵盖形状、纹理以及颜色这三大关键特征。其中,对于纹理特征的提取,采用灰度梯度共生矩阵这一方法来实现。通过灰度梯度共生矩阵,可以有效地捕捉图像中像素灰度值之间在不同方向和距离上的相互关系,进而量化地反映出图像的纹理特性,为后续的图像分析、分类等任务提供重要的纹理信息依据。
该数据集为2010-2023年中国A股上市公司管理层情感语调的年度面板数据,覆盖45,320条样本,数据源自年报及半年报的"管理层讨论与分析"部分。通过构建中文金融情感词典(融合《知网情感分析用词典》与L&M金融词汇表),采用文本分析方法计算情感语调指标,包括:正面/负面词汇数量、文本相似度、情感语调1((积极词-消极词)/总词数)和情感语调2((积极词-消极词)/(积极词+消极词))。同时包含盈利预测偏差、审计意见类型等衍生指标,可用于研究信息披露质量、市场反应及代理问题。该数据复刻了《管理世界》《财经研究》等期刊的变量构建方法,被应用于分析语调操纵对债券市场的影响,学术常用度与稀缺度较高。
cmd-bat-批处理-脚本-FTIME.zip
1747829038637.png
2025年自动化X光检查机项目大数据研究报告.docx
在计算机组成原理课程设计中,我全程跟随老师的指导,独立完成了以下两项任务:一是利用Logisim软件进行原码一位乘法器的仿真设计,通过逐步搭建电路、配置逻辑单元,实现了原码乘法运算的完整流程,深入理解了原码乘法的原理和实现机制;二是完成了补码一位乘法器的Logisim仿真,同样按照老师讲解的步骤,精心设计电路,确保补码乘法运算的正确性,进一步掌握了补码乘法的运算规则和电路实现方法。通过这两个项目,我不仅巩固了理论知识,还提升了动手实践能力和逻辑思维能力。
cmd-bat-批处理-脚本-msvc2017.zip
cmd-bat-批处理-脚本-virtualcam-install.zip
二十四节气之立秋介绍.pptx
cmd-bat-批处理-脚本-shift.zip
二十四节气之小雪介绍.pptx
java、SpringBoot面试专题,6页面试题
cmd-bat-批处理-脚本-GenerateUnionWinMD.zip
二十四节气之大暑节气.pptx
python实现五子棋游戏源码
cmd-bat-批处理-脚本-TransparentConsole.zip
cmd-bat-批处理-脚本-ppcp.zip