Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/ExecWithLotsOfArgs.java
41153 views
/*1* Copyright (c) 1997, 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 403356025@summary 4033560 limited args of exec to 198 on Solaris. We check26that we can actually exec more args than that.27@author Anand Palaniswamy28@run main/othervm ExecWithLotsOfArgs29*/3031import java.io.BufferedReader;32import java.io.InputStreamReader;33import java.io.File;34import java.io.IOException;3536public class ExecWithLotsOfArgs {3738public static class EchoingHelper {39public static void main(String[] args) {40for (int i = 0; i < args.length; i++) {41System.out.println(args[i]);42}43}44}4546public static void main(String[] args) throws Exception {47String[] command = new String[300];48int n = 0;4950/*51* The Java program to exec. This is slightly fragile. Works52* on Solaris and Win32.53*/54command[n++] = System.getProperty("java.home") + File.separator +55"bin" + File.separator + "java";56if (System.getProperty("java.class.path") != null) {57command[n++] = "-classpath";58command[n++] = System.getProperty("java.class.path");59}6061/*62* The class with main() that the exec'd VM will run.63*/64command[n++] = "ExecWithLotsOfArgs$EchoingHelper";6566/*67* Make a long set of args n, n + 1, ... , 300.68*/69for (int i = n; i < command.length; i++) {70command[i] = new String(new Integer(i).toString());71}7273/*74* Do the exec.75*/76Process p = null;77p = Runtime.getRuntime().exec(command);78BufferedReader in = new BufferedReader79(new InputStreamReader(p.getInputStream()));8081/*82* Read back all the strings and that the same were returned.83*/84String s;85int count = n;86while ((s = in.readLine()) != null) {87if (count >= command.length) {88failed("Was expecting " + (command.length - 2) +89" strings to be echo'ed back, but got " +90(count - 1) + " instead");91}92if (!s.equals(command[count])) {93failed("Exec'd process returned \"" +94s + "\", was expecting \"" +95command[count] + "\"");96}97count++;98}99100/*101* Did we read anything at all?102*/103if (count == n) {104/* Try reading the error stream to see if we got any diagnostics */105in = new BufferedReader(new InputStreamReader(p.getErrorStream()));106while ((s = in.readLine()) != null) {107System.err.println("Error output: " + s);108}109failed("Exec'd process didn't writing anything to its stdout");110}111}112113private static void failed(String s) {114throw new RuntimeException("Failed: " + s);115}116}117118119