14
14
15
15
/**
16
16
* 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.
18
18
* <p>
19
19
* This class provides a simple way to start other Java programs. By default,
20
20
* the Java executable, the classpath, the working directory and the environment
21
21
* are the same as for the current Java process, but can be changed if desired.
22
22
* <p>
23
23
* 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>.
25
29
* <p>
26
30
* The design of this class' interface mostly follows the {@link ProcessBuilder}
27
31
* class.
@@ -306,7 +310,7 @@ public JavaProcessBuilder autoExit(final boolean exit) {
306
310
* subprocesses. See {@link System#getenv(String)}" */
307
311
308
312
/**
309
- * Creates a {@link ProcessBuilder} with a command that reflects the current
313
+ * Builds a {@link ProcessBuilder} with a command that reflects the current
310
314
* settings of this builder. Changes subsequently made to this instance are
311
315
* not reflected in the returned process builder.
312
316
* <p>
@@ -315,20 +319,20 @@ public JavaProcessBuilder autoExit(final boolean exit) {
315
319
*
316
320
* @return A process builder
317
321
*/
318
- public ProcessBuilder create () {
322
+ public ProcessBuilder build () {
319
323
return new ProcessBuilder (javaCommand ());
320
324
}
321
325
322
326
/**
323
- * Convenience method that {@linkplain #create () creates } a process builder
327
+ * Convenience method that {@linkplain #build () builds } a process builder
324
328
* and {@linkplain ProcessBuilder#start() starts} a process.
325
329
*
326
330
* @return The started process
327
331
* @throws IOException
328
332
* If one is thrown by the {@link ProcessBuilder#start()} method
329
333
*/
330
334
public Process start () throws IOException {
331
- return create ().start ();
335
+ return build ().start ();
332
336
}
333
337
334
338
/**
@@ -344,7 +348,23 @@ public List<String> javaCommand() {
344
348
if (autoExit )
345
349
command .add (AutoExitProgram .class .getName ());
346
350
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
+
348
363
return command ;
349
364
}
365
+
366
+ private static boolean isWindows () {
367
+ return System .getProperty ("os.name" ).toLowerCase ()
368
+ .startsWith ("windows" );
369
+ }
350
370
}
0 commit comments