Java 获取当前类文件所在的路径,java当前,package net.
分享于 点击 44481 次 点评:24
Java 获取当前类文件所在的路径,java当前,package net.
package net.oschina.tester;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, "/"); }}//该片段来自于http://byrx.net
用户点评