public class JarUtil { private String jarName; private String jarPath; public JarUtil(Class clazz) { String path = clazz.getProtectionDomain().getCodeSource().getLocation() .getFile(); try { path = java.net.URLDecoder.decode(path, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); }
java.io.File jarFile = new java.io.File(path); this.jarName = jarFile.getName();
java.io.File parent = jarFile.getParentFile(); if (parent != null) { this.jarPath = parent.getAbsolutePath(); } }
/** * 獲取Class類所在Jar包的名稱 * * @return Jar包名 (例如:C:\temp\demo.jar 則返回 demo.jar ) */ public String getJarName() { try { return java.net.URLDecoder.decode(this.jarName, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
/** * 取得Class類所在的Jar包路徑 * * @return 返回一個路徑 (例如:C:\temp\demo.jar 則返回 C:\temp ) */ public String getJarPath() { try { return java.net.URLDecoder.decode(this.jarPath, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
public static void main(String[] args) throws Exception { JarUtil ju = new JarUtil(JarUtil.class); System.out.println("Jar name: " + ju.getJarName()); System.out.println("Jar path: " + ju.getJarPath()); } }
|