需要知道執行jar包時,jar包所在的路徑。
開始使用了
p.getClass().getResource("/").getPath();
結果在IDE里面使用是好的,但是在命令行中執行時,會報NullPointerException錯誤。
接着嘗試使用另一種方式,
File f = new File(""); String cf = null; try { cf = f.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); } System.out.println(cf);
成功。