转自:http://kinganpo.iteye.com/blog/876243

import java.io.File;  
/** 
 * 获取打包后jar的路径信息 
 * @author Administrator 
 *  2011-01-16 13:53:12 
 */  
public class JarTool {  
    //获取jar绝对路径  
    public static String getJarPath(){  
        File file = getFile();  
        if(file==null)return null;  
         return file.getAbsolutePath();  
    }  
    //获取jar目录  
    public static String getJarDir() {  
        File file = getFile();  
        if(file==null)return null;  
         return getFile().getParent();  
    }  
    //获取jar包名  
    public static String getJarName() {  
        File file = getFile();  
        if(file==null)return null;  
        return getFile().getName();  
    }  
  
    private static File getFile() {  
        //关键是这行...  
        String path = JarTool.class.getProtectionDomain().getCodeSource()  
                .getLocation().getFile();  
        try{  
            path = java.net.URLDecoder.decode(path, "UTF-8");//转换处理中文及空格  
        }catch (java.io.UnsupportedEncodingException e){  
            return null;  
        }  
        return new File(path);  
    }  
      
} 

  

相关文章:

  • 2021-11-23
  • 2021-11-23
  • 2021-07-10
  • 2021-10-20
  • 2021-05-23
  • 2021-08-20
猜你喜欢
  • 2021-12-12
  • 2021-06-22
  • 2022-12-23
  • 2022-12-23
  • 2021-07-03
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案