diff --git a/src/java.base/share/classes/java/lang/Runtime.java b/src/java.base/share/classes/java/lang/Runtime.java index 7948ebffb6372..2288aca4d1114 100644 --- a/src/java.base/share/classes/java/lang/Runtime.java +++ b/src/java.base/share/classes/java/lang/Runtime.java @@ -290,6 +290,12 @@ public void halt(int status) { * behaves in exactly the same way as the invocation * {@link #exec(String, String[], File) exec}{@code (command, null, null)}. * + * @deprecated This method is error-prone and should not be used, the corresponding method + * {@link #exec(String[])} or {@link ProcessBuilder} should be used instead. + * The command string is broken into tokens using only whitespace characters. + * For an argument with an embedded space, such as a filename, this can cause problems + * as the token does not include the full filename. + * * @param command a specified system command. * * @return A new {@link Process} object for managing the subprocess @@ -311,6 +317,7 @@ public void halt(int status) { * @see #exec(String[], String[], File) * @see ProcessBuilder */ + @Deprecated(since="18") public Process exec(String command) throws IOException { return exec(command, null, null); } @@ -324,6 +331,12 @@ public Process exec(String command) throws IOException { * behaves in exactly the same way as the invocation * {@link #exec(String, String[], File) exec}{@code (command, envp, null)}. * + * @deprecated This method is error-prone and should not be used, the corresponding method + * {@link #exec(String[], String[])} or {@link ProcessBuilder} should be used instead. + * The command string is broken into tokens using only whitespace characters. + * For an argument with an embedded space, such as a filename, this can cause problems + * as the token does not include the full filename. + * * @param command a specified system command. * * @param envp array of strings, each element of which @@ -352,6 +365,7 @@ public Process exec(String command) throws IOException { * @see #exec(String[], String[], File) * @see ProcessBuilder */ + @Deprecated(since="18") public Process exec(String command, String[] envp) throws IOException { return exec(command, envp, null); } @@ -374,6 +388,12 @@ public Process exec(String command, String[] envp) throws IOException { * produced by the tokenizer are then placed in the new string * array {@code cmdarray}, in the same order. * + * @deprecated This method is error-prone and should not be used, the corresponding method + * {@link #exec(String[], String[], File)} or {@link ProcessBuilder} should be used instead. + * The command string is broken into tokens using only whitespace characters. + * For an argument with an embedded space, such as a filename, this can cause problems + * as the token does not include the full filename. + * * @param command a specified system command. * * @param envp array of strings, each element of which @@ -406,6 +426,7 @@ public Process exec(String command, String[] envp) throws IOException { * @see ProcessBuilder * @since 1.3 */ + @Deprecated(since="18") public Process exec(String command, String[] envp, File dir) throws IOException { if (command.isEmpty()) diff --git a/test/jdk/java/lang/ProcessBuilder/Zombies.java b/test/jdk/java/lang/ProcessBuilder/Zombies.java index 25299608ea70d..5a98250bce712 100644 --- a/test/jdk/java/lang/ProcessBuilder/Zombies.java +++ b/test/jdk/java/lang/ProcessBuilder/Zombies.java @@ -49,17 +49,20 @@ public static void main(String[] args) throws Throwable { final Runtime rt = Runtime.getRuntime(); try { - rt.exec("no-such-file"); + String[] cmd = {"no-such-file"}; + rt.exec(cmd); throw new Error("expected IOException not thrown"); } catch (IOException expected) {/* OK */} try { - rt.exec("."); + String[] cmd = {"."}; + rt.exec(cmd); throw new Error("expected IOException not thrown"); } catch (IOException expected) {/* OK */} try { - rt.exec(TrueCommand, null, new File("no-such-dir")); + String[] cmd = {TrueCommand}; + rt.exec(cmd, null, new File("no-such-dir")); throw new Error("expected IOException not thrown"); } catch (IOException expected) {/* OK */} diff --git a/test/jdk/java/lang/RuntimeTests/exec/BadEnvp.java b/test/jdk/java/lang/RuntimeTests/exec/BadEnvp.java index 1a5c9f8c9be63..d216b65a84094 100644 --- a/test/jdk/java/lang/RuntimeTests/exec/BadEnvp.java +++ b/test/jdk/java/lang/RuntimeTests/exec/BadEnvp.java @@ -29,6 +29,7 @@ public class BadEnvp { + @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { Runtime r = Runtime.getRuntime(); java.io.File dir = new java.io.File("."); diff --git a/test/jdk/java/lang/RuntimeTests/exec/ExecWithDir.java b/test/jdk/java/lang/RuntimeTests/exec/ExecWithDir.java index a52e2221ca883..4f2b723d18085 100644 --- a/test/jdk/java/lang/RuntimeTests/exec/ExecWithDir.java +++ b/test/jdk/java/lang/RuntimeTests/exec/ExecWithDir.java @@ -41,7 +41,7 @@ public static void main(String args[]) throws Exception { } UnixCommands.ensureCommandsAvailable("true"); - final String trueCmd = UnixCommands.findCommand("true"); + final String[] trueCmd = {UnixCommands.findCommand("true")}; File dir = new File("."); for (int i = 1; i <= N; i++) { System.out.print(i); diff --git a/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java b/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java index 347c4d0eae378..a457b48b5e55f 100644 --- a/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java +++ b/test/jdk/java/lang/RuntimeTests/exec/SetCwd.java @@ -62,6 +62,7 @@ public void testRuntimeExecWithArray() throws Exception { @Test public void testRuntimeExecWithString() throws Exception { String cmd = String.join(" ", CMD_ARRAY); + @SuppressWarnings("deprecation") Process process = Runtime.getRuntime().exec(cmd, null, new File(TEST_CLASSES)); verifyProcessOutput(process);