path不以’/'開頭時,默認是從此類所在的包下取資源; path 以’/'開頭時,則是從ClassPath根下獲取;
什麼意思呢?看下面這段代碼的輸出結果就明白了:
package testpackage; public class TestMain { public static void main(String[] args) { System.out.println(TestMain.class.getResource("")); System.out.println(TestMain.class.getResource("/")); } }
輸出結果:
file:/E:/workspace/Test/bin/testpackage/
file:/E:/workspace/Test/bin/