如何获取jar包的在执行机上面的路径

背景: 最近在项目中遇到一个小问题, 几行代码就能解决了

1 String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile();
2         try {
3             path = java.net.URLDecoder.decode(path, "UTF-8");
4         } catch (UnsupportedEncodingException e) {
5             e.printStackTrace();
6         }
7         String jarPath = new File(path).getParentFile().getAbsolutePath();

猜你喜欢

转载自www.cnblogs.com/andrew209/p/9249946.html