当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

[简单]代码片段_2

发表于: 2014-11-17   作者:53873039oycg   来源:转载   浏览:
摘要:       import java.io.File; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.Stri

     

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class 打印Jar_S3_Test {
	public static void main(String[] args) {
		打印Jar_S3_Test t = new 打印Jar_S3_Test();
		t.getProjectsAllJarsFullPath();
		System.out.println("----------------------------");
		List<String> jarList = t.getProjectsAllJars();
		for (String sub : jarList) {
			System.out.println(sub);
		}
	}

	/**
	 * @Description: 打印jar名称
	 * @see: http://www.mkyong.com/java/how-to-print-out-the-current-project-
	 *       classpath/
	 */
	public void getProjectsAllJarsFullPath() {
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		URL[] urls = ((URLClassLoader) cl).getURLs();
		for (URL url : urls) {
			System.out.println(url.getFile());
		}
	}

	/**
	 * @Description: 打印jar名称
	 * @see: http://www.mkyong.com/java/how-to-print-out-the-current-project-
	 *       classpath/
	 */
	public List<String> getProjectsAllJars() {
		ClassLoader cl = ClassLoader.getSystemClassLoader();
		URL[] urls = ((URLClassLoader) cl).getURLs();
		List<String> jarList = new ArrayList<String>();
		for (URL url : urls) {
			if (!StringUtils.isBlank(getJarName(url.getFile()))) {
				jarList.add(getJarName(url.getFile()));
			}
		}
		return jarList;
	}

	public String getJarName(String path) {
		path = path.replace("\\", "/");
		path = path.replace("/", File.separator);
		if (path.lastIndexOf(File.separator) == -1) {
			return path;
		} else {
			return path.substring(path.lastIndexOf(File.separator) + 1);
		}
	}
}

    全文完

[简单]代码片段_2

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号