获取当前 jar 包的路径

来源:互联网 发布:有趣的电子产品知乎 编辑:程序博客网 时间:2024/06/02 17:47
最开始我是用能够这种方式来获取当前 jar 包的文件的:

private static String LOCATION = [MainClass].class.getProtectionDomain()
        .getCodeSource().getLocation().getFile();


但是如果路径中包含中文,就会变成带“%xx%xx”格式的字符串。所以应该这样写才对:

private static String LOCATION;

static {
    try {
        LOCATION 
= URLDecoder.decode([MainClass].class.getProtectionDomain()
            .getCodeSource().getLocation().getFile(), “UTF
-8″);
    } 
catch (UnsupportedEncodingException e) {
        LOCATION 
= “”;
    }