try {//获取当前类所在路径String path = new AlgApplicationTests().getClass().getProtectionDomain().getCodeSource().getLocation().getPath();//解决中文路径path = .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();}
如果觉得《Java获取jar包所在路径》对你有帮助,请点赞、收藏,并留下你的观点哦!