zoukankan      html  css  js  c++  java
  • java中获得jar包执行路径的方法

      当我们由于某种需要需要的得到jar的路径是可以用下面的方式来获得:

          basePath = new Solution().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
            basePath = URLDecoder.decode(basePath,"utf-8");       
            System.out.println("basePaht:"+basePath);
            if(basePath.endsWith(".jar")){
                basePath = basePath.substring(0,basePath.lastIndexOf("/")+1);
            }    
        File f = new File(basePath);    
        basePath = f.getAbsolutePath(); //得到windows下的正确路径 

      其中Solution为main函数所在类的类名称,如果路径中有中文的话需要用URLDecoder.decode(basePath,"utf-8")utf-8进行转码,其中得到的basePath即为jar包的父路径,到现在为止,得到的路径还不是Windows下的格式,应用f.getAbsolutePath()就可得到。

  • 相关阅读:
    Redis
    元类 metaclass
    聊一聊 Django 中间件
    Django rest framework
    聊一聊python的单例模式
    Django-admin管理工具
    MongoDB
    Beautifulsoup
    三、模型(一)
    九、Python发送QQ邮件(SMTP)
  • 原文地址:https://www.cnblogs.com/yueliming/p/3162667.html
Copyright © 2011-2022 走看看