获取Java程序运行的路径 | 获取当前jar包的路径

经过试验,不管是否是Jar包,不管是否是Tomcat部署,以下三个方法均可实现。

 

package test;

 

public class MyPath {

    public static String getProjectPath() {

 

       java.net.URL url = MyPath.class.getProtectionDomain().getCodeSource().getLocation();

       String filePath = null;

       try {

           filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");

       } catch (Exception e) {

           e.printStackTrace();

       }

    if (filePath.endsWith(".jar"))

       filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);

    java.io.File file = new java.io.File(filePath);

    filePath = file.getAbsolutePath();

    return filePath;

}

   

    public static String getRealPath() {

       String realPath = MyPath.class.getClassLoader().getResource("").getFile();

       java.io.File file = new java.io.File(realPath);

       realPath = file.getAbsolutePath();

       try {

           realPath = java.net.URLDecoder.decode(realPath, "utf-8");

       } catch (Exception e) {

           e.printStackTrace();

       }

      

       return realPath;

    }

   

    public static String getAppPath(Class<?> cls){ 

       //检查用户传入的参数是否为空 

        if(cls==null)  

        throw new java.lang.IllegalArgumentException("参数不能为空!"); 

       

        ClassLoader loader=cls.getClassLoader(); 

        //获得类的全名,包括包名 

        String clsName=cls.getName();

        //此处简单判定是否是Java基础类库,防止用户传入JDK内置的类库 

        if(clsName.startsWith("java.")||clsName.startsWith("javax.")) {

        throw new java.lang.IllegalArgumentException("不要传送系统类!");

        }

        //将类的class文件全名改为路径形式

        String clsPath= clsName.replace(".", "/")+".class"; 

 

        //调用ClassLoader的getResource方法,传入包含路径信息的类文件名 

        java.net.URL url =loader.getResource(clsPath); 

        //从URL对象中获取路径信息 

        String realPath=url.getPath(); 

        //去掉路径信息中的协议名"file:" 

        int pos=realPath.indexOf("file:"); 

        if(pos>-1) {

        realPath=realPath.substring(pos+5); 

        }

        //去掉路径信息最后包含类文件信息的部分,得到类所在的路径 

        pos=realPath.indexOf(clsPath); 

        realPath=realPath.substring(0,pos-1); 

        //如果类文件被打包到JAR等文件中时,去掉对应的JAR等打包文件名 

        if(realPath.endsWith("!")) {

        realPath=realPath.substring(0,realPath.lastIndexOf("/"));

        }

        java.io.File file = new java.io.File(realPath);

        realPath = file.getAbsolutePath();

      

        try

        realPath=java.net.URLDecoder.decode(realPath,"utf-8"); 

        }catch(Exception e){

        throw new RuntimeException(e);

        } 

        return realPath; 

    }//getAppPath定义结束 

}

 

使用Jar包,在Tomcat的运行结果如下:

ProjectPath: D:\J2EE\Tomcat 6.0\webapps\MyService1WebP\WEB-INF\lib

RealPath: D:\J2EE\Tomcat 6.0\webapps\MyService1WebP\WEB-INF\classes

Apppath: D:\J2EE\Tomcat 6.0\webapps\MyService1WebP\WEB-INF\classes

转载于:https://www.cnblogs.com/snailrun/archive/2012/08/25/2657023.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值