打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Java获取Jar包路径
http://hi.baidu.com/matrix286/blog/item/fe6d8cfbdffae915a8d311b2.html
Java获取Jar包路径
2010年01月09日 星期六 17:09
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());
}
}
(#)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
为什么Java项目前会出现一个红色感叹号!
关于系统读取properties配置文件的路径问题,包括打成jar包的运行文件
统计java代码行数和jar包中*.class代码的行数
springboot 打jar 包部署时 读取外部配置文件
在Jar包中打入DLL文件并调用的方式
使用hadoop eclipse plugin提交Job并添加多个第三方jar(完美版)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服