执行cmd
例:在C盘中启动E盘下的jar文件

程序运行时获取到: E:\work\code\project\RuDngApplication-0.0.1-SNAPSHOT.jar
示例代码
public class JarPath{
public static String getPath(){
URL url=JarPath.class.getProtectionDomain().getCodeSource().getLocation();
String path=null;
try{
//转化为utf-8编码,支持中文
path=URLDecoder.decode(url.getPath(),"utf-8");
System.out.println(path);//结果:E:\work\code\project\RuDngApplication-0.0.1-SNAPSHOT.jar
}catch(Exceptione){
e.printStackTrace();
}
}
}
本文介绍了一种在Java程序中获取当前运行jar文件完整路径的方法,通过使用getClass().getProtectionDomain().getCodeSource().getLocation()获取URL,再经由URLDecoder.decode进行解码,最终得到包含中文路径的jar文件绝对路径。
8407

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



