简体   繁体   中英

Run a jar file out of java programm with many system properties

I want to start a.jar File out of my java program using the Process Builder like this:

ProcessBuilder pb = new ProcessBuilder("java",  "-Xdebug", "-DpropKey1=value", "-DpropKey2=value", "MyJar.jar");
Process p = pb.start();

I am looking for a smart way to pass a high amount of the system properties my java program is using to the ProcessBuilder. Right at this moment I am doing it like this:

StringBuilder d1 = new StringBuilder(100);
d1.append("-DpropKey1=");
d1.append(System.getProperty("propKey1"));
String d1Str = d1.toString();

StringBuilder d2 = new StringBuilder(100);
d2.append("-DpropKey2=");
d2.append(System.getProperty("propKey2"));
String d2Str = d2.toString();

ProcessBuilder pb = new ProcessBuilder("java",  "-Xdebug", d1Str, d2Str, "MyJar.jar");
Process p = pb.start();

But this way doesn't seem very smart to me. It's just I have a lot of system properties I want to pass out of the java programm (more than 10). It doesn't feel right to use a StringBuilder for every system property I want to pass.

Try this:

String[] params=... 

new ProcessBuilder(params).start()

Params is an array of parameters as String.

Old:

Write a method, which does the trick. As argument it could get a Map<String, String>.

The key of an entry is the parameter name, and the value of an entry is the parameter value.

The method builds the String in a loop, which is iterating over the entry set of the map.

Sorry, I read false! This will not work this way with processbuilder!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM