欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java获取程序运行路径、获取当前jar路径,程序运行jar,public class

来源: javaer 分享于  点击 47853 次 点评:47

java获取程序运行路径、获取当前jar路径,程序运行jar,public class


public class RtuMain {    public static void main(String[] args) {        System.out.println(getProjectPath());        System.out.println(getRealPath());    }    public static String getProjectPath() throws UnsupportedEncodingException {        URL url = RtuMain.class.getProtectionDomain().getCodeSource().getLocation();        String filePath = URLDecoder.decode(url.getPath(), "utf-8");        if (filePath.endsWith(".jar")) {            filePath = filePath.substring(0, filePath.lastIndexOf("/") + 1);        }        File file = new File(filePath);        filePath = file.getAbsolutePath();        return filePath;    }    public static String getRealPath() {        String realPath = RtuMain.class.getClassLoader().getResource("").getFile();        File file = new File(realPath);        realPath = file.getAbsolutePath();        try {            realPath = URLDecoder.decode(realPath, "utf-8");        } catch (Exception e) {            e.printStackTrace();        }        return realPath;    }}
相关栏目:

用户点评