在运行中的jar中,用用代码获取jar包的位置。
1、在普通的jar包中使用String path = MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
可以获取到这个file:/Users/xxx/xxx-0.0.1.jar!/BOOT-INF/classes!/
路径。
2、springboot中使用下面的方法:
ApplicationHome home = new ApplicationHome(MyApplication.class);
//返回jar包所在的目录 /Users/xxx/
home.getDir();
//返回java包文件位置 /Users/xxx/xxx-0.0.1.jar
home.getSource(); // returns the jar absolute path.
参考文档:
https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file
https://stackoverflow.com/questions/46657181/how-to-get-spring-boot-application-jar-parent-folder-path-dynamically
来源:oschina
链接:https://my.oschina.net/kunBlog/blog/4456911