Skip to content

Commit 7e73bca

Browse files
author
Roger Riggs
committed
8276408: Deprecate Runtime.exec methods with a single string command line argument
Reviewed-by: alanb
1 parent 75adf54 commit 7e73bca

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

src/java.base/share/classes/java/lang/Runtime.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ public void halt(int status) {
290290
* behaves in exactly the same way as the invocation
291291
* {@link #exec(String, String[], File) exec}{@code (command, null, null)}.
292292
*
293+
* @deprecated This method is error-prone and should not be used, the corresponding method
294+
* {@link #exec(String[])} or {@link ProcessBuilder} should be used instead.
295+
* The command string is broken into tokens using only whitespace characters.
296+
* For an argument with an embedded space, such as a filename, this can cause problems
297+
* as the token does not include the full filename.
298+
*
293299
* @param command a specified system command.
294300
*
295301
* @return A new {@link Process} object for managing the subprocess
@@ -311,6 +317,7 @@ public void halt(int status) {
311317
* @see #exec(String[], String[], File)
312318
* @see ProcessBuilder
313319
*/
320+
@Deprecated(since="18")
314321
public Process exec(String command) throws IOException {
315322
return exec(command, null, null);
316323
}
@@ -324,6 +331,12 @@ public Process exec(String command) throws IOException {
324331
* behaves in exactly the same way as the invocation
325332
* {@link #exec(String, String[], File) exec}{@code (command, envp, null)}.
326333
*
334+
* @deprecated This method is error-prone and should not be used, the corresponding method
335+
* {@link #exec(String[], String[])} or {@link ProcessBuilder} should be used instead.
336+
* The command string is broken into tokens using only whitespace characters.
337+
* For an argument with an embedded space, such as a filename, this can cause problems
338+
* as the token does not include the full filename.
339+
*
327340
* @param command a specified system command.
328341
*
329342
* @param envp array of strings, each element of which
@@ -352,6 +365,7 @@ public Process exec(String command) throws IOException {
352365
* @see #exec(String[], String[], File)
353366
* @see ProcessBuilder
354367
*/
368+
@Deprecated(since="18")
355369
public Process exec(String command, String[] envp) throws IOException {
356370
return exec(command, envp, null);
357371
}
@@ -374,6 +388,12 @@ public Process exec(String command, String[] envp) throws IOException {
374388
* produced by the tokenizer are then placed in the new string
375389
* array {@code cmdarray}, in the same order.
376390
*
391+
* @deprecated This method is error-prone and should not be used, the corresponding method
392+
* {@link #exec(String[], String[], File)} or {@link ProcessBuilder} should be used instead.
393+
* The command string is broken into tokens using only whitespace characters.
394+
* For an argument with an embedded space, such as a filename, this can cause problems
395+
* as the token does not include the full filename.
396+
*
377397
* @param command a specified system command.
378398
*
379399
* @param envp array of strings, each element of which
@@ -406,6 +426,7 @@ public Process exec(String command, String[] envp) throws IOException {
406426
* @see ProcessBuilder
407427
* @since 1.3
408428
*/
429+
@Deprecated(since="18")
409430
public Process exec(String command, String[] envp, File dir)
410431
throws IOException {
411432
if (command.isEmpty())

test/jdk/java/lang/ProcessBuilder/Zombies.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ public static void main(String[] args) throws Throwable {
4949
final Runtime rt = Runtime.getRuntime();
5050

5151
try {
52-
rt.exec("no-such-file");
52+
String[] cmd = {"no-such-file"};
53+
rt.exec(cmd);
5354
throw new Error("expected IOException not thrown");
5455
} catch (IOException expected) {/* OK */}
5556

5657
try {
57-
rt.exec(".");
58+
String[] cmd = {"."};
59+
rt.exec(cmd);
5860
throw new Error("expected IOException not thrown");
5961
} catch (IOException expected) {/* OK */}
6062

6163
try {
62-
rt.exec(TrueCommand, null, new File("no-such-dir"));
64+
String[] cmd = {TrueCommand};
65+
rt.exec(cmd, null, new File("no-such-dir"));
6366
throw new Error("expected IOException not thrown");
6467
} catch (IOException expected) {/* OK */}
6568

test/jdk/java/lang/RuntimeTests/exec/BadEnvp.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
public class BadEnvp {
3131

32+
@SuppressWarnings("deprecation")
3233
public static void main(String[] args) throws Exception {
3334
Runtime r = Runtime.getRuntime();
3435
java.io.File dir = new java.io.File(".");

test/jdk/java/lang/RuntimeTests/exec/ExecWithDir.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static void main(String args[]) throws Exception {
4141
}
4242
UnixCommands.ensureCommandsAvailable("true");
4343

44-
final String trueCmd = UnixCommands.findCommand("true");
44+
final String[] trueCmd = {UnixCommands.findCommand("true")};
4545
File dir = new File(".");
4646
for (int i = 1; i <= N; i++) {
4747
System.out.print(i);

test/jdk/java/lang/RuntimeTests/exec/SetCwd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void testRuntimeExecWithArray() throws Exception {
6262
@Test
6363
public void testRuntimeExecWithString() throws Exception {
6464
String cmd = String.join(" ", CMD_ARRAY);
65+
@SuppressWarnings("deprecation")
6566
Process process = Runtime.getRuntime().exec(cmd, null,
6667
new File(TEST_CLASSES));
6768
verifyProcessOutput(process);

0 commit comments

Comments
 (0)