2012-09-29 113 views
0

我有一個項目結構,其資源文件夾與src處於同一級別。資源文件夾有一個子文件夾配置,它有一個配置文件conffile.ttl。從eclipse運行程序時,我已將資源包含在classpath中。訪問位於src文件夾外的jar文件中的資源

在閱讀從我的主類的conffile.ttl文件,我用

URL url =TestPropertyFilePath.class.getClassLoader().getSystemResource("conf/conffile.ttl"); 
    System.out.println(url.getPath()); 

工作正常。但是,當我把它捆在一個罐子裏,並試圖運行罐子使用

java -jar testprop.jar TestPropertyFilePath 

Java找不到conffile.ttl文件。我想請教如何從jar中訪問conffile.ttl。

+1

嘗試[TestPropertyFilePath.class.getRescource( 「/ ...」)](http://docs.oracle.com/javase/7/docs/api/java/ lang/Class.html#getResource(java.lang.String))而不是 – MadProgrammer

+0

是jar文件中的ttl文件嗎? – Thilo

+0

是的。當我做捆綁時,我包括資源文件夾。所以jar文件包含類和相同級別的資源的包。 – Nimonika

回答

0

試試這個:

TestPropertyFilePath.class.getClassLoader().getSystemResource("conf.conffile"); 
相關問題