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