2012-03-02 119 views
0

我有在tomcat上運行的web應用程序。我想投入一些資源是否可以將資源文件放入JAR文件中?

resources.jar 

例如,

configuration.xml 

但是當我嘗試:

new File("/conf/configuration.xml") 

也沒有找到。

可能我應該以另一種方式配置路徑?

+0

你在應用程序的類路徑中配置了jar嗎? – developer 2012-03-02 07:50:08

+1

可能是[jar文件中的加載文件]重複(http://stackoverflow.com/questions/4548699/load-file-within-a-jar) – home 2012-03-02 07:51:44

+0

你是什麼是否在類路徑中?這個jar包在webapps/mywebapplication/WEB-INF/lib中。我認爲這個文件夾中的所有jar都在classpath中。 – user710818 2012-03-02 07:51:54

回答

2

當在一個web應用程序運行時,你應該使用ContextClassLoader

Thread.currentThread().getContextClassLoader().getResource("/conf/configuration.xml"); 

Thread.currentThread().getContextClassLoader().getResourceAsStream("/conf/configuration.xml"); 

第一個將返回一個URL,第二個的InputStream

相關問題