/**
* 获取工程所在目录
* @return
* @throws UnsupportedEncodingException
*/
private static String getProjectPath() throws UnsupportedEncodingException{
URL url = [ThisClass].class.getProtectionDomain().getCodeSource().getLocation();
String filePath = URLDecoder.decode(url.getPath(), "UTF-8");
if(filePath.endsWith(".jar"))
filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
return filePath;
}
本文介绍了一种在Java中获取当前工程路径的方法。通过利用类的保护域获取代码源位置,并对其进行解码处理,确保路径正确无误。特别地,针对打包成jar文件的情况进行了额外处理。

被折叠的 条评论
为什么被折叠?



