提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
从加载的类获取Jar路径
//package com.nowjava; /* nowjava - 时 代 Java 提 供 */ import java.net.URL; import java.util.Enumeration; public class Main { public static void main(String[] argv) throws Exception { String pkg = "nowjava.com"; System.out.println(getJarPath(pkg)); } public static URL getJarPath(String pkg) { try { Enumeration<URL> resources = Thread.currentThread() .getContextClassLoader() /** NowJava.com - 时代Java 提供 **/ .getResources(pkg.replace('.', '/')); while (resources.hasMoreElements()) { URL url = (URL) resources.nextElement(); return url; } return null; } catch (