1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.io.File; import java.net.URISyntaxException; import java.security.CodeSource; import java.util.Objects; /** * www.gaohaiyan.com * jar工具。用于获取当 java -jar *.jar 时,jar所在的位置 */ public class JarUtil { private JarUtil() { } /** * 获取java程序的jar的路径 * clazz:main函数所在类的class * @return */ public static String getProgramPath(Class clazz) { CodeSource codeSource = clazz.getProtectionDomain().getCodeSource(); File jarFile = null; try { jarFile = new File(codeSource.getLocation().toURI().getPath()); String currentJarPath = Objects.requireNonNull(jarFile, "源码文件须存在").getParentFile().getPath(); System.out.println("源码文件路径:" + currentJarPath); return currentJarPath; } catch (URISyntaxException e) { e.printStackTrace(); System.out.println("[SRC]源码路径error:" + e.getMessage()); } return null; } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/4124.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设