獲取jar包當前的路徑


轉自: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);  
    }  
      
} 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM