Here you can find the source of findContainingJar(Class> clazz)
public static String findContainingJar(Class<?> clazz)
//package com.java2s; /*//from w w w . j av a 2 s . c o m * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.IOException; import java.net.URL; import java.net.URLDecoder; import java.util.Enumeration; public class Main { public static String findContainingJar(Class<?> clazz) { return findContainingJar(clazz, null); } /** * Load the first jar library contains clazz with preferJarKeyword matched. If preferJarKeyword is null, just load the * jar likes Hadoop Commons' ClassUtil * @param clazz * @param preferJarKeyWord * @return */ public static String findContainingJar(Class<?> clazz, String preferJarKeyWord) { ClassLoader loader = clazz.getClassLoader(); String classFile = clazz.getName().replaceAll("\\.", "/") + ".class"; try { Enumeration e = loader.getResources(classFile); URL url = null; do { if (!e.hasMoreElements()) { if (url == null) return null; else break; } url = (URL) e.nextElement(); if (!"jar".equals(url.getProtocol())) break; if (preferJarKeyWord != null && url.getPath().indexOf(preferJarKeyWord) != -1) break; if (preferJarKeyWord == null) break; } while (true); String toReturn = url.getPath(); if (toReturn.startsWith("file:")) { toReturn = toReturn.substring("file:".length()); } toReturn = URLDecoder.decode(toReturn, "UTF-8"); return toReturn.replaceAll("!.*$", ""); } catch (IOException var6) { throw new RuntimeException(var6); } } }