2013-12-16 77 views
1

我已經使用tomcat成功部署了我的java web服務。此Web服務訪問配置文件(的.properties)我已經放在以下目錄中的config.properties文件如何從我的Java Web服務訪問.properties文件

C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ProcurementWS1\WEB-INF\classes\MainPackagePr 

ProcurmentWS1是WAR文件

這是一個如何我嘗試訪問的名稱它從我的代碼:

Properties properties = new Properties(); 
      InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("procwsconfig.properties"); 
      properties.load(is); 

而我得到的負載一個空指針異常

你能告訴我做錯了嗎?

+0

1)把你的屬性文件內項目(在WAR文件內)。 2)把它放在類路徑上的某個地方 – user1933888

+0

那麼我使用eclipse,並且我將這個文件包含在java構建路徑中,並且它沒有工作 – user1912404

回答

8

把它放在src/config中直接以配置包的源,在構建它會去/WEB-INF/classes/config/config.properties

this.getClass().getResourceAsStream("/config/config.properties");

enter image description here

然後我在具有方法的同一個項目中創建了一個服務類。

public static InputStream getConfigAsInputStream(){ 
    return Service.class.getResourceAsStream("/config/config.properties"); 
} 
0

您可以用放置在某處你的類路徑上和你的.java源文件中的任何文件夾下:

ResourceBundle bundle1 =   ResourceBundle.getBundle("com.witness.web.license.l10n.VersionsFileName"); 
    String fileName = bundle1.getString("11.1"); 

com.witness.web.license.l10n是你的包