Path: blob/master/test/jdk/java/lang/RuntimeTests/exec/Duped.java
41153 views
/*1* Copyright (c) 1999, 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 418042925@summary Lossage in dup2 if System.in is closed.26@run main/othervm Duped27*/2829import java.io.BufferedReader;30import java.io.PrintStream;31import java.io.InputStreamReader;32import java.io.File;3334public class Duped {3536public static class Echo {37public static void main(String args[]) throws Exception {38StringBuffer s = new StringBuffer();39int c;40while ((c = System.in.read()) != -1)41s.append((char)c);42System.out.println(s);43}44}4546public static void main(String[] args) throws Exception {4748String command =49System.getProperty("java.home") +50File.separator +51"bin" +52File.separator +53"java -classpath " +54System.getProperty("java.class.path") +55" Duped$Echo";5657if (args.length == 1 && args[0].equals("-dont")) {58/*59* To quickly check that this test is working when it is60* supposed to, just run it with -dont and it shouldn't61* complain at all.62*/63} else {64/*65* In normal runs we just close in, and that causes66* lossage on fork.67*/68System.in.close();69}7071Process p = Runtime.getRuntime().exec(command);72PrintStream out = new PrintStream(p.getOutputStream());73out.println(HELLO);74out.close();7576BufferedReader in =77new BufferedReader(new InputStreamReader(p.getInputStream()));78String read = in.readLine();7980if (!HELLO.equals(read)) {81throw new Exception("Failed, read ``" + read + "''");82}83}8485static final String HELLO = "Hello, world!";8687}888990