Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 89211c7

Browse files
committed
Implemented workaround for empty-arg-bug in ProcessBuilder
1 parent 4621548 commit 89211c7

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/main/java/ch/trick17/javaprocesses/JavaProcessBuilder.java

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@
1414

1515
/**
1616
* A class to build {@link ProcessBuilder}s for Java processes. So, yes,
17-
* instances of this class are builder builders.
17+
* instances of this class are <em>builder</em> builders.
1818
* <p>
1919
* This class provides a simple way to start other Java programs. By default,
2020
* the Java executable, the classpath, the working directory and the environment
2121
* are the same as for the current Java process, but can be changed if desired.
2222
* <p>
2323
* Note that this class assumes that the Java executable is located at
24-
* <code><em>[java_home]</em>/bin/java</code>.
24+
* <code><em>[java_home]</em>/bin/java</code>. Further, this class has a
25+
* built-in workaround for the erroneous handling of empty command line
26+
* arguments on Windows. See <a
27+
* href="http://bugs.java.com/view_bug.do?bug_id=6518827"
28+
* >http://bugs.java.com/view_bug.do?bug_id=6518827</a>.
2529
* <p>
2630
* The design of this class' interface mostly follows the {@link ProcessBuilder}
2731
* class.
@@ -306,7 +310,7 @@ public JavaProcessBuilder autoExit(final boolean exit) {
306310
* subprocesses. See {@link System#getenv(String)}" */
307311

308312
/**
309-
* Creates a {@link ProcessBuilder} with a command that reflects the current
313+
* Builds a {@link ProcessBuilder} with a command that reflects the current
310314
* settings of this builder. Changes subsequently made to this instance are
311315
* not reflected in the returned process builder.
312316
* <p>
@@ -315,20 +319,20 @@ public JavaProcessBuilder autoExit(final boolean exit) {
315319
*
316320
* @return A process builder
317321
*/
318-
public ProcessBuilder create() {
322+
public ProcessBuilder build() {
319323
return new ProcessBuilder(javaCommand());
320324
}
321325

322326
/**
323-
* Convenience method that {@linkplain #create() creates} a process builder
327+
* Convenience method that {@linkplain #build() builds} a process builder
324328
* and {@linkplain ProcessBuilder#start() starts} a process.
325329
*
326330
* @return The started process
327331
* @throws IOException
328332
* If one is thrown by the {@link ProcessBuilder#start()} method
329333
*/
330334
public Process start() throws IOException {
331-
return create().start();
335+
return build().start();
332336
}
333337

334338
/**
@@ -344,7 +348,23 @@ public List<String> javaCommand() {
344348
if(autoExit)
345349
command.add(AutoExitProgram.class.getName());
346350
command.add(mainClass);
347-
command.addAll(args);
351+
352+
if(isWindows())
353+
/* Workaround for a bug in ProcessBuilder:
354+
* http://bugs.java.com/view_bug.do?bug_id=6518827 */
355+
for(final String arg : args)
356+
if(arg.isEmpty())
357+
command.add("\"\"");
358+
else
359+
command.add(arg);
360+
else
361+
command.addAll(args);
362+
348363
return command;
349364
}
365+
366+
private static boolean isWindows() {
367+
return System.getProperty("os.name").toLowerCase()
368+
.startsWith("windows");
369+
}
350370
}

0 commit comments

Comments
 (0)