CommandLines { public static String escape(List<String> commandLine) { StringBuilder stringBuilder = new StringBuilder(1024); boolean first = true; for (String element: commandLine) { if (first) { first = false; } else { stringBuilder.append(' '); } stringBuilder.append(SINGLE_WHITESPACE_PATTERN.matcher(element).replaceAll("\\\\$1")); } return stringBuilder.toString(); } private  CommandLines(); static String escape(List<String> commandLine);  }
@Test public void escapeTest() { Assert.assertEquals(CommandLines.escape(Collections.singletonList("foo")), "foo"); Assert.assertEquals(CommandLines.escape(Collections.singletonList("foo bar")), "foo\\ bar"); Assert.assertEquals(CommandLines.escape(Arrays.asList("foo", "bar")), "foo bar"); Assert.assertEquals(CommandLines.escape(Arrays.asList("foo", "bar baz")), "foo bar\\ baz"); }