duke@1: /* ohair@554: * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: /* @test duke@1: * @bug 5070898 duke@1: * @summary javah command doesn't throw correct exit code in case of error duke@1: */ duke@1: duke@1: import java.io.*; duke@1: import java.util.*; duke@1: import javax.tools.*; duke@1: duke@1: public class T5070898 duke@1: { duke@1: public static void main(String... args) throws Exception { duke@1: new T5070898().run(); duke@1: } duke@1: duke@1: public void run() throws Exception { duke@1: writeFile(); duke@1: compileFile(); duke@1: duke@1: int rc = runJavah(); duke@1: System.err.println("exit code: " + rc); duke@1: if (rc == 0) duke@1: throw new Exception("unexpected exit code: " + rc); duke@1: } duke@1: duke@1: void writeFile() throws Exception { duke@1: String content = duke@1: "package test;\n" + duke@1: "public class JavahTest{\n" + duke@1: " public static void main(String args){\n" + duke@1: " System.out.println(\"Test Message\");" + duke@1: " }\n" + duke@1: " private static native Object nativeTest();\n" + duke@1: "}\n"; duke@1: FileWriter out = new FileWriter("JavahTest.java"); duke@1: try { duke@1: out.write(content); duke@1: } finally { duke@1: out.close(); duke@1: } duke@1: } duke@1: duke@1: void compileFile() throws Exception { duke@1: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); duke@1: int rc = javac.run(null, null, null, "JavahTest.java"); duke@1: if (rc != 0) duke@1: throw new Exception("compilation failed"); duke@1: } duke@1: duke@1: int runJavah() throws Exception { duke@1: List cmd = new ArrayList(); duke@1: File java_home = new File(System.getProperty("java.home")); duke@1: if (java_home.getName().equals("jre")) duke@1: java_home = java_home.getParentFile(); duke@1: cmd.add(new File(new File(java_home, "bin"), "javah").getPath()); duke@1: duke@1: // ensure we run with the same bootclasspath as this test, duke@1: // in case this test is being run with -Xbootclasspath duke@1: cmd.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path")); duke@1: duke@1: cmd.add("JavahTest"); duke@1: duke@1: ProcessBuilder pb = new ProcessBuilder(cmd); duke@1: pb.redirectErrorStream(true); duke@1: pb.environment().remove("CLASSPATH"); duke@1: Process p = pb.start(); duke@1: p.getOutputStream().close(); duke@1: duke@1: String line; duke@1: DataInputStream in = new DataInputStream(p.getInputStream()); duke@1: try { duke@1: while ((line = in.readLine()) != null) duke@1: System.err.println(line); duke@1: } finally { duke@1: in.close(); duke@1: } duke@1: duke@1: return p.waitFor(); duke@1: } duke@1: }