Here you can find the source of getClassPathFile(Class> clazz)
Parameter | Description |
---|---|
clazz | the clazz |
public static File getClassPathFile(Class<?> clazz)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.net.URL; public class Main { /**//from w w w. j a v a 2s . com * Gets the class path file. * * @param clazz * the clazz * @return the class path file */ public static File getClassPathFile(Class<?> clazz) { File file = getClassFile(clazz); for (int i = 0, count = clazz.getName().split("[.]").length; i < count; i++) file = file.getParentFile(); if (file.getName().toUpperCase().endsWith(".JAR!")) { file = file.getParentFile(); } return file; } /** * Gets the class file. * * @param clazz * the clazz * @return the class file */ public static File getClassFile(Class<?> clazz) { URL path = clazz.getResource(clazz.getName().substring(clazz.getName().lastIndexOf(".") + 1) + ".classs"); if (path == null) { String name = clazz.getName().replaceAll("[.]", "/"); path = clazz.getResource("/" + name + ".class"); } return new File(path.getFile()); } }