jar包冲突解决方法

时间:2021-02-20 07:11:02

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;
        }
        
    }
}