获取运行的jar包的路径。

自古美人都是妖i 提交于 2020-10-12 02:51:46

在运行中的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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!