import javax . tools . Tool ; import java . net . URLDecoder ; //获取类路径 public final class test132 { public static String getClassPath (){ String path = test132 . class . getClassLoader (). getResource ( "" ). getPath (). toString
import javax.tools.Tool;
import java.net.URLDecoder;
//获取类路径
public final class test132 {
public static String getClassPath(){
String path=test132.class.getClassLoader().getResource( "" ).getPath().toString();
try {
return URLDecoder.decode( path,"UTF-8" );
}catch (Exception e){
e.printStackTrace();
}
return path;
}
}
上面编写方法定义一个实现类
import javax.tools.Tool;
//获取文件程序类的路径
public class test133 {
public static void main(String[] args){
String classPath=test132.getClassPath();
System.out.println( classPath );
}
}
运行结果