T5070898.java revision 2933:49d207bf704d
1/* 2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* @test 25 * @bug 5070898 26 * @summary javah command doesn't throw correct exit code in case of error 27 * @modules java.compiler 28 * jdk.compiler 29 */ 30 31import java.io.*; 32import java.util.*; 33import javax.tools.*; 34 35public class T5070898 36{ 37 public static void main(String... args) throws Exception { 38 new T5070898().run(); 39 } 40 41 public void run() throws Exception { 42 writeFile(); 43 compileFile(); 44 45 int rc = runJavah(); 46 System.err.println("exit code: " + rc); 47 if (rc == 0) 48 throw new Exception("unexpected exit code: " + rc); 49 } 50 51 void writeFile() throws Exception { 52 String content = 53 "package test;\n" + 54 "public class JavahTest{\n" + 55 " public static void main(String args){\n" + 56 " System.out.println(\"Test Message\");" + 57 " }\n" + 58 " private static native Object nativeTest();\n" + 59 "}\n"; 60 FileWriter out = new FileWriter("JavahTest.java"); 61 try { 62 out.write(content); 63 } finally { 64 out.close(); 65 } 66 } 67 68 void compileFile() throws Exception { 69 JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); 70 int rc = javac.run(null, null, null, "JavahTest.java"); 71 if (rc != 0) 72 throw new Exception("compilation failed"); 73 } 74 75 int runJavah() throws Exception { 76 List<String> cmd = new ArrayList<String>(); 77 File java_home = new File(System.getProperty("java.home")); 78 cmd.add(new File(new File(java_home, "bin"), "javah").getPath()); 79 80 cmd.add("JavahTest"); 81 82 ProcessBuilder pb = new ProcessBuilder(cmd); 83 pb.redirectErrorStream(true); 84 pb.environment().remove("CLASSPATH"); 85 Process p = pb.start(); 86 p.getOutputStream().close(); 87 88 String line; 89 DataInputStream in = new DataInputStream(p.getInputStream()); 90 try { 91 while ((line = in.readLine()) != null) 92 System.err.println(line); 93 } finally { 94 in.close(); 95 } 96 97 return p.waitFor(); 98 } 99} 100