Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ArgWithSpaceAndFinalBackslash.java
41153 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 479465225* @summary Ensure that a command argument that contains a space and a final26* backslash is handled correctly27*/2829import java.io.*;30import java.util.*;313233public class ArgWithSpaceAndFinalBackslash {3435private static String getJavaCommand() {36String javaHome = System.getProperty("java.home");37if (javaHome != null && javaHome.length() > 0)38return (javaHome39+ File.separatorChar + "bin"40+ File.separatorChar + "java");41else42return "java";43}4445public static void main(String[] args) throws Exception {4647if (args.length > 0) {48System.err.println(args[0]);49return;50}5152String[] cmd = new String[5];53int i = 0;54cmd[i++] = getJavaCommand();55cmd[i++] = "-cp";56String cp = System.getProperty("test.classes");57if (cp == null)58cp = ".";59cmd[i++] = cp;60cmd[i++] = "ArgWithSpaceAndFinalBackslash";61cmd[i++] = "foo bar\\baz\\";6263Process process = Runtime.getRuntime().exec(cmd);64InputStream in = process.getErrorStream();65byte[] buf = new byte[1024];66int n = 0, d;67while ((d = in.read(buf, n, buf.length - n)) >= 0)68n += d;69String s = new String(buf, 0, n, "US-ASCII").trim();70if (!s.equals(cmd[i - 1]))71throw new Exception("Test failed: Got \"" + s72+ "\", expected \"" + cmd[i - 1] + "\"");73}7475}767778