2017-01-16 254 views
1

我們最近必須從頭開始設置一個tomcat服務器。 Tomcat版本是8.0.20。部署一個war文件,現在System.getProperty("mode")返回「null」,它應該返回PREPROD。System.getProperty(「mode」)返回「null」

它應該從位於webapps目錄中的mode.properties文件讀取此「模式」。這兩行註釋掉了另一部分代碼,這些代碼在新的tomcat服務器上不再有效。我用應該工作的代碼替換它。

//String pathOfWebInf = sce.getServletContext().getRealPath("WEB-INF"); 
//String pathOfLocalhostFile = pathOfWebInf + File.separator + "classes" 
//  + File.separator; 
String pathOfLocalhostFile = this.getClass().getResource("/").getPath(); 

String mode = System.getProperty("mode"); 
String fileName = "localhost-oracle.properties." + mode; 

StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
encryptor.setPassword("xxx"); 

Properties dbProps = new EncryptableProperties(encryptor); 
try 
{ 
    InputStream is = new FileInputStream(pathOfLocalhostFile + fileName); 
    dbProps.load(is); 
} catch (Exception e) 
{ 
    throw new IOException("Could not read properties file " + pathOfLocalhostFile + fileName); 
} 
+1

我建議把你的屬性變成catalina.properties在服務器/ conf文件夾。 –

+0

我解決了問題,通過恢復到Tomcat 7,Tomcat 8有問題,如http://stackoverflow.com/questions/32197494/why-does-servletcontext-getrealpath-returns-null-on-tomcat-8 – Adder

回答

1

你必須首先加載mode.properties,像這樣

private Properties mode=null; 
mode = new Properties(); 
mode.load(new FileInputStream(pathtoMODE)); 

String mode = mode.getProperty("mode"); 
+0

理想情況下,我不必硬編碼路徑,也許它應該檢查屬性文件的類路徑。另外,我想對代碼和配置進行一些最小的更改,因爲「舊」代碼正在部署鏈中的「舊」tomcats上工作。 – Adder

2

System.properties有關在JVM運行的計算機的所有屬性...有定義有沒有模式關鍵,這就是爲什麼你會得到null作爲值....

檢查出所有的PC性能做:

final Properties props = System.getProperties(); 
props.list(System.out); 

和驗證自己,沒有在地圖模式關鍵...

+0

我的問題,然後會是:我可以更改什麼,以便它從webapps/mode.properties文件中讀取屬性,就像它在舊的Tomcat上一樣? – Adder