訪問文件在我的Maven項目:從類路徑
src/main/java/App --> has Main
src/main/resources/file.json
。
public class App
{
public static void main(String[] args)
{
URL url = App.class.getClass().getResource("/file.json");
File file = new File(url.getPath());
據我瞭解這些Maven的docs,我應該能夠訪問file.json
,因爲它(應該是)在類路徑中,因爲它位於src/main/resources
。
但是,我在url.getPath()
調用上得到NullPointerException
。
你沒有領先的'/'試過嗎? – Powerlord
是的,但沒有運氣 –
我總是使用類加載器的'getResource()'方法。另外,你如何執行'App.main'? – disrvptor