java获取程序运行路径、获取当前jar路径
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; } }