获取jar包所在的路径
输入比较常用的功能,Mark一下;在window下面路径的最开始可能会是一个斜杠,需要手动去除一下。
public String SysPath()
{
String path=this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
if(path.toUpperCase().indexOf(".JAR")!=-1){
try{
//截取".JAR第一次出现前的字符串"
String StrPath=path.substring(0, path.toUpperCase().indexOf(".jar".toUpperCase()));
//获取“.jar”包的上一层文件夹
path=StrPath.substring(0,StrPath.lastIndexOf("/")+1);
}
catch(Exception e){
return "出错了:"+e.toString();
}
}
//第一个字符可能是斜杠,在Windows系统中去掉
Properties props = System.getProperties();
String osName = props.getProperty("os.name");
System.out.println( "os name" + osName);
if(path != null && !"".equals(path)){
if(path.substring(0,1).equals("\\") || path.substring(0,1).equals("/")){
path = path.substring(1);
}
}
return path;
}

本文介绍了一种在Java中获取JAR包所在路径的方法,包括处理路径中的特殊字符和适应不同操作系统路径格式的问题。
2963

被折叠的 条评论
为什么被折叠?



