博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jar包冲突解决方法
阅读量:4571 次
发布时间:2019-06-08

本文共 3504 字,大约阅读时间需要 11 分钟。

import java.io.File;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ClearConflict
{
    
    List<ClassJarPair> classList = new ArrayList<ClassJarPair>();
    
    List<String> jarList = new ArrayList<String>();
    
    /**
     * 生成输出格式
     *
     * @param name 输出的文件名或目录名
     * @param level 输出的文件名或者目录名所在的层次
     * @return 输出的字符串
     */
    public String createPrintStr(String name, int level)
    {
        // 输出的前缀
        String printStr = "";
        // 按层次进行缩进
        for (int i = 0; i < level; i++)
        {
            printStr = printStr + "  ";
        }
        printStr = printStr + "- " + name;
        return printStr;
    }
    
    /**
     * 输出初始给定的目录
     *
     * @param dirPath 给定的目录
     */
    public void printDir(String dirPath)
    {
        // 将给定的目录进行分割
        String[] dirNameList = dirPath.split("\\\\");
        // 按格式输出
        for (int i = 0; i < dirNameList.length; i++)
        {
            System.out.println(createPrintStr(dirNameList[i], i));
        }
    }
    
    /**
     * 输出给定目录下的文件,包括子目录中的文件
     *
     * @param dirPath 给定的目录
     */
    public void readFile(String dirPath)
    {
        // 建立当前目录中文件的File对象
        File file = new File(dirPath);
        // 取得代表目录中所有文件的File对象数组
        File[] list = file.listFiles();
        // 遍历file数组
        for (int i = 0; i < list.length; i++)
        {
            if (list[i].isDirectory())
            {
                // 递归子目录
                readFile(list[i].getPath());
            }
            else
            {
                if (list[i].isFile())
                {
                    try
                    {
                        if ((list[i].getCanonicalFile() + "").endsWith(".jar"))
                        {
                            // System.out.println(list[i].getCanonicalFile()+"");
                            JarFile jar = new JarFile(list[i].getCanonicalFile() + "");
                            Enumeration<JarEntry> entrys = jar.entries();
                            while (entrys.hasMoreElements())
                            {
                                JarEntry jarEntry = entrys.nextElement();
                                if (jarEntry.getName().endsWith("class"))
                                {
                                    int index =
                                        classList.indexOf(new ClassJarPair(list[i].getCanonicalFile() + "",
                                            jarEntry.getName() + ""));
                                    if (0 < index)
                                    {
                                        String jar1 = list[i].getCanonicalFile() + "";
                                        String jar2 = classList.get(index).getJarName();
                                        if (jarList.contains(list[i].getCanonicalFile() + ""))
                                        {
                                            ;
                                        }
                                        else
                                        {
                                            System.out.printf("%20s CONFILT WITH JAR: %20s",
                                                jar2.substring(jar2.lastIndexOf("\\") + 1),
                                                jar1.substring(jar1.lastIndexOf("\\") + 1));
                                            jarList.add(list[i].getCanonicalFile() + "");
                                            System.out.print();
                                        }
                                        /*
                                         * System.out.print("the jar: "+list[i].getCanonicalFile()+" confilt with Jar: "
                                         * + classList.get(index).getJarName());
                                         * System.out.println("has the same class: "+jarEntry.getName());
                                         */
                                    }
                                    else
                                    {
                                        classList.add(new ClassJarPair(list[i].getCanonicalFile() + "",
                                            jarEntry.getName() + ""));
                                    }
                                }
                                
                            }
                        }
                        
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args)
        throws IOException
    {
        File dir = null;
        if (0 == args.length)
        {
            dir = new File("");
        }
        else if (1 == args.length)
        {
            dir = new File(args[0]);
        }
        else
        {
            System.out.println("Usage: java ClearConflict libpath");
        }
        System.out.println(dir.getAbsoluteFile());
        ClearConflict cl = new ClearConflict();
        String dirPath = dir.getAbsoluteFile() + "";
        cl.readFile(dirPath);
    }
    
    class ClassJarPair
    {
        private String jarName;
        
        private String className;
        
        ClassJarPair(String jarName, String className)
        {
            this.jarName = jarName;
            this.className = className;
        }
        
        @Override
        public boolean equals(Object obj)
        {
            ClassJarPair classJarPair = (ClassJarPair)obj;
            return className.equals(classJarPair.getClassName());
        }
        
        public String getJarName()
        {
            return jarName;
        }
        
        public void setJarName(String jarName)
        {
            this.jarName = jarName;
        }
        
        public String getClassName()
        {
            return className;
        }
        
        public void setClassName(String className)
        {
            this.className = className;
        }
        
    }
}

转载于:https://www.cnblogs.com/guoyuqiangf8/p/3489525.html

你可能感兴趣的文章
常用的JS技术1
查看>>
商品搜索
查看>>
upc 9519 New Game
查看>>
oracle 用sql实现密码的加密,解密
查看>>
各种蒟蒻模板【如此简单】
查看>>
搜索1016
查看>>
配置算法(第4版)的Java编译环境
查看>>
PostgreSQL 9.6.0 手册
查看>>
编写带有点击特效的UIButton
查看>>
使用mac版思维导图软件MindNode
查看>>
理解面向对象编程(OOP Object Oriented Programming)
查看>>
[題解]luogu_P1144最短路計數
查看>>
每日一个linux命令5 -- rm
查看>>
外存===内存
查看>>
node.js中的fs.appendFile方法使用说明
查看>>
URAL 1297 求最长回文字符串
查看>>
HDU 1098 Ignatius's puzzle 费马小定理+扩展欧几里德算法
查看>>
【C/C++】指针
查看>>
Anconda3导入TensorFlow报错,错误:h5py\__init__.py:36: FutureWarning
查看>>
js中return;、return true、return false;区别
查看>>