第一种方式:使用类路径
String ProjectPath = XXX(this).class.getProtectionDomain().getCodeSource().getLocation().getPath();
如果直接执行.class文件那么会得到当前class的绝对路径;如果封装在jar包里面执行jar包那么会得到当前jar包的绝对路径。
如果路径包含Unicode字符,还需要将路径转码:
path = java.net.URLDecoder.decode(path, "UTF-8");
第二种方式:使用JVM
String path = System.getProperty("java.class.path");