2012-10-30 26 views
0

有沒有辦法在Java命令行中嵌套系統屬性?例如,指定類似於:Java命令行中的嵌套系統屬性

java -DworkingDir=/tmp -DconfigFile=${workingDir}/someFile.config

我的目標是用類似的東西在Eclipse中Tomcat的啓動配置(Tomcat的補丁爲記錄與SLF4J /的logback):

-Dcatalina.base="C:\data\workspaces\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0" 
-Dlogback.configurationFile="${catalina.base}\conf\logback.groovy"`. 

回答

2

在Java中沒有辦法讓擴展透明地發生。但你可以這樣做:

$ workingDir=/tmp 
$ java -DworkingDir=${workingDir} -DconfigFile=${workingDir}/someFile.config. 

換句話說,讓shell在調用Java之前進行擴展。 (在Windows批處理文件的語法是不同的......但這個想法是一樣的。)


順便說一句,如果你運行一個命令是這樣的:

$ java -DworkingDir=/tmp -DconfigFile=${workingDir}/someFile.config 

一個POSIX shell將將${workingDir}解釋爲shell變量擴展。如果沒有定義變量workingDir,則這將擴展爲空,因此您需要使用引號將${workingDir}轉換爲實際的Java屬性值;例如

$ java -DworkingDir=/tmp -DconfigFile=\${workingDir}/someFile.config 
+1

+1。這完全落在殼牌的下水道里。雖然提問者顯然是使用Windows(注意'C:\'開始的路徑),所以我假設他將不得不使用批處理腳本的版本變量和替換。 –

+0

所以沒有辦法讓自然擴張沒有技巧。對於我的預期用法無效(在Eclipse啓動配置中進行擴展)。謝謝你的回答,Stephen C和iccthedral的回答都是可以接受的,Tom Anderson的評論是相關的。 – PomCompot

2

當然,只要確保你閱讀並正確地更換,所以例如:

java -DworkingDir="/tmp" -DconfigFile="${workingDir}/someFile.config"

Properties props = System.getProperties(); 
String wDir = props.getProperty("workingDir"); 
String configFile = props.getProperty("configFile").replace("${workingDir}", wDir); 
System.out.println(configFile); 

你的想法...