如果类是打包在jar文件里,那这个方法就无能为力,只能想其他辙
ResourcePathTester.java
import java.net.URL; import org.apache.commons.lang.StringUtils; /** * 类路径获取 * @author Winter Lau * @date 2009-12-4 下午03:29:43 */ public class ResourcePathTester { public static void main(String[] args) { System.out.println(getResourcePath()); } private static String getResourcePath() { String className = ResourcePathTester.class.getName(); String classNamePath = className.replace(".", "/") + ".class"; URL is = ResourcePathTester.class.getClassLoader().getResource(classNamePath); String path = is.getFile(); path = StringUtils.replace(path, "%20", " "); return StringUtils.removeStart(path, "/"); } }
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/10373.html