2010-11-12 101 views
3

我創建了一個包含三個配置文件如何獲取正確的配置文件路徑?

  • 的log4j.xml
  • QueueConfig.xml
  • rabbitmq.properties

我就把這三個文件到一個資源文件夾的Java項目。

然後我創建了我的項目的jar文件。這個jar文件被添加到另一個項目中。它無法找到配置文件的正確位置。

我的文件結構:

alt text

感謝


回答

3

你可以用它喜歡。

ClassFromWhichYouAreACcessingTheseFiles.class.getResources("resources/log4j.properties"); 

比如果您將此jar添加到另一個項目,您將能夠訪問它。

+0

thanks.i嘗試「包路徑」,但它返回空值。 URL url = this.getClass()。getResource(「resources/log4j.xml」); – 2010-11-12 11:55:59

+0

@sam不要使用這個,請嘗試'ClassFromWhichYouAreACcessingTheseFiles.class.getResources(「/ resources/log4j.properties」);' – 2010-11-12 12:03:16

1

如果你的配置文件的WEB-INF/classes文件夾最終

ClassFromWhichYouAreACcessingTheseFiles.class.getClassLoader().getResources("log4j.properties"); 

否則包括來自ClassFromWhichYouAreACcessingTheseFiles

+0

謝謝,它works.it的需要我的jar文件包含資源文件夾。 – 2010-11-12 12:01:46