Here you can find the source of getJarFile(@Nonnull Class> clazz)
public static File getJarFile(@Nonnull Class<?> clazz)
//package com.java2s; /*/*from ww w. j av a 2 s . co m*/ * @(#)$Id$ * * Copyright 2006-2008 Makoto YUI * * Licensed 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. * * Contributors: * Makoto YUI - initial implementation */ import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URL; import java.net.URLDecoder; import javax.annotation.Nonnull; public class Main { public static File getJarFile(@Nonnull Class<?> clazz) { File file = getClassFile(clazz); String path = file.getPath(); final int idx = path.lastIndexOf('!'); if (idx == -1) { return null; } String jarFilePath = path.substring(0, idx); if (jarFilePath.startsWith("file:\\")) {// workaround for windows jarFilePath = jarFilePath.substring(6); } else if (jarFilePath.startsWith("file:/")) { jarFilePath = jarFilePath.substring(5); } file = new File(jarFilePath); return file; } @SuppressWarnings("deprecation") @Nonnull public static File getClassFile(@Nonnull Class<?> clazz) { String className = clazz.getName(); String path = getRelativeClassFilePath(className); URL url = clazz.getResource('/' + path); String absolutePath = url.getFile(); String decoded = URLDecoder.decode(absolutePath); return new File(decoded); } public static File getClassFile(@Nonnull Class<?> clazz, ClassLoader cl) { String className = clazz.getName(); String path = getRelativeClassFilePath(className); URL url = cl.getResource('/' + path); String absolutePath = url.getFile(); final String decoded; try { decoded = URLDecoder.decode(absolutePath, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException(e); } return new File(decoded); } @Nonnull public static String getRelativeClassFilePath(@Nonnull String className) { return className.replace('.', '/') + ".class"; } }