0

Is there a way to nest system properties in a Java command line? For instance, specifying something like:

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

I aim to use something like that for the Tomcat launch configuration in Eclipse (Tomcat patched to log with SLF4J/Logback) :

-Dcatalina.base="C:\data\workspaces\EclipseWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0" 
-Dlogback.configurationFile="${catalina.base}\conf\logback.groovy"`.
Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143
Doc Davluz
  • 4,154
  • 5
  • 30
  • 32

2 Answers2

2

Sure, just make sure you read and replace it correctly, so for example:

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);

you get the idea...

nullpotent
  • 9,162
  • 1
  • 31
  • 42
2

There is no way to get the expansion to happen transparently ... in Java. But you could do this:

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

In other words, get the shell to do the expansion before invoking Java. (The syntax in a Windows batch file would be different ... but the idea is the same.)


Incidentally, if you run a command like this:

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

a POSIX shell will interpret ${workingDir} as a shell variable expansion. If no workingDir variable is defined, this expands to nothing ... so you would need to use quoting to get the ${workingDir} into the actual Java property value; e.g.

$ java -DworkingDir=/tmp -DconfigFile=\${workingDir}/someFile.config
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 1
    +1. This falls squarely in the shell's bailiwick. Although the asker is evidently using Windows (note the path starting `C:\`), so i assume he will have to use batch script's version of variables and substitution. – Tom Anderson Oct 30 '12 at 17:53
  • So no way of getting a natural expansion without tricks. Would not work for my expected usage (expansion in a Eclipse launch config). Thanks your answers, both Stephen C and iccthedral responses are acceptable ones and Tom Anderson comment is relevant. – Doc Davluz Nov 22 '12 at 09:57