执行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();
}
}
}