码迷,mamicode.com
首页 > 编程语言 > 详细

java如何在项目依赖的jar包中查找冲突的类

时间:2016-05-06 12:31:19      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

原文:java如何在项目依赖的jar包中查找冲突的类

源代码下载地址:http://www.zuidaima.com/share/1785617759915008.htm

package com.whaty.util;

import java.util.*;
import java.io.*;

public class JarClassFind {
	public static int count = 0;

	static public void main(String[] args) {
		/*
		 * if (args.length < 2) { showHowToUsage(); return; }
		 */
		String className = "StringUtil"; // 要查询的class,要带包名的类名
		String libPath = "E:\\work\\workspace\\jngl_mysql\\WebRoot\\WEB-INF\\lib\\"; // 所要查找的JAR包的目录

		String absoluteclassname = className.replace(‘.‘, ‘/‘) + ".class";

		System.out.println("Find class [" + className + "] in Path [" + libPath + "] Results:");
		FindClassInLocalSystem(libPath, absoluteclassname);
		if (JarClassFind.count == 0) {
			System.out.println("Error:Can‘t Find Such Jar File!");
		}
		System.out.println("Find Process Ended! Total Results:" + JarClassFind.count);
	}

	private static void FindClassInLocalSystem(String path, String classname) {
		if (path.charAt(path.length() - 1) != ‘\\‘) {
			path += ‘\\‘;
		}
		File file = new File(path);
		if (!file.exists()) {
			System.out.println("Error: Path not Existed! Please Check it out!");
			return;
		}
		String[] filelist = file.list();
		for (int i = 0; i < filelist.length; i++) {
			File temp = new File(path + filelist[i]);
			if ((temp.isDirectory() && !temp.isHidden() && temp.exists())) {
				FindClassInLocalSystem(path + filelist[i], classname);
			} else {
				if (filelist[i].endsWith("jar")) {
					try {
						java.util.jar.JarFile jarfile = new java.util.jar.JarFile(path + filelist[i]);
						for (Enumeration e = jarfile.entries(); e.hasMoreElements();) {
							String name = e.nextElement().toString();
							if (name.equals(classname) || name.indexOf(classname) > -1) {
								System.out.println("No." + ++JarClassFind.count);
								System.out.println("Jar Package:" + path + filelist[i]);
								System.out.println(name);
							}
						}
					} catch (Exception eee) {
					}
				}
			}
		}
	}

	public static void showHowToUsage() {
		System.out.println("Usage: Java -cp. JarClassFind <source path> <source class name>");
		System.out.println("Usage: Java -classpath. JarClassFind <source path> <source class name>");
		System.out.println("");
		System.out.println("<source path>:\t\tPath to Find eg:D:\\Jbuilder");
		System.out.println("<source class name>:\tClass to Find eg:java.applet.Applet");
	}
}

效果如下图:

技术分享

 


最代码官方编辑于2014-7-3 23:41:12

java如何在项目依赖的jar包中查找冲突的类

标签:

原文地址:http://blog.csdn.net/yaerfeng/article/details/51329021

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!