我的項目目錄結構:使用Maven ClassLoader.getSystemClassLoader()getResource()方法返回(在Eclipse)空
MyProject/
src/main/Java
src/main/resources
stories
file.story
在主類,我有以下線返回的返回null,同時通過MAVEN
excuting主類String folderName = "stories";
URL appURL = ClassLoader.getSystemClassLoader().getResource(folderName);
在通過maven執行時,appURL返回NULL。
通過閱讀Stackoverflow上的一篇文章,我得到了kanow,我在服務器上運行webapp,但沒有引用服務器上的資源,所以我們需要在POM.xml文件中添加一些代碼。我已經添加下面的代碼在pom.xml文件,但仍其不工作:(
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>stories</include>
</includes>
</resource>
</resources>
尋找幫助。
你可以嘗試用這種替代的getClass()getClassLoader()的getResource(... )替代ClassLoader.getSystemClassLoader()。getResource(...) – Pradeep
因爲「無法對非靜態方法進行靜態引用」而出錯,因爲上面的代碼是主要的方法。 – Hema
嗨,你需要創建你的類的對象,如 YourClass a = new YourClass();然後你需要調用a.getClass()。getClassLoader()。getResource(...) – Pradeep