Java获取jar包所在路径

        try {
            //获取当前类所在路径
            String path = new AlgApplicationTests().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
            //解决中文路径
            path = java.net.URLDecoder.decode(path,"UTF-8");
            //把路径切换到jar包所在位置
            File file = new File(path);
            path = file.getParentFile().getParentFile().getParentFile().getParent();
            //路径里可能会以file:\E:\xxx\ 显示 需要过虑出正确地址
            path = path.replace("file:\\","").replace("file:","");
            System.out.println(path);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

版权声明:本文为weixin_47951400原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。