62,614
社区成员
发帖
与我相关
我的任务
分享
//得到jar包的位置。比如:c:\test.jar
public static String getJREPath(){
URL url;
// 得到jar程序的路径
url = FileUtil.class.getProtectionDomain().getCodeSource().getLocation();
// 将url路径转码,主要应用于汉字
String temp = "";
try {
temp = URLDecoder.decode(url.getFile(), "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
return temp;
}
//得到jar同目录的文件路径,参数是文件的名称
public static String getJARPathFile(String fileName){
return new File(getJREPath()).getParent()+File.separator+fileName;
}
System.out.println(YourClass.class.getProtectionDomain().getCodeSource().getLocation());