Domanda

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"`.
È stato utile?

Soluzione

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

Altri suggerimenti

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...

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top