T5070898.java revision 2721:f7ce2cfa4cdb
1113229Smdodd/*
2255765Sdes * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
3113229Smdodd * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4113229Smdodd *
5113229Smdodd * This code is free software; you can redistribute it and/or modify it
6113229Smdodd * under the terms of the GNU General Public License version 2 only, as
7113229Smdodd * published by the Free Software Foundation.
8113229Smdodd *
9113229Smdodd * This code is distributed in the hope that it will be useful, but WITHOUT
10113229Smdodd * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11113229Smdodd * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12113229Smdodd * version 2 for more details (a copy is included in the LICENSE file that
13113229Smdodd * accompanied this code).
14113229Smdodd *
15113229Smdodd * You should have received a copy of the GNU General Public License version
16113229Smdodd * 2 along with this work; if not, write to the Free Software Foundation,
17113229Smdodd * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18113229Smdodd *
19113229Smdodd * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20113229Smdodd * or visit www.oracle.com if you need additional information or have any
21113229Smdodd * questions.
22113229Smdodd */
23113229Smdodd
24113229Smdodd/* @test
25113229Smdodd * @bug 5070898
26113229Smdodd * @summary javah command doesn't throw correct exit code in case of error
27113229Smdodd */
28255765Sdes
29113229Smdoddimport java.io.*;
30113229Smdoddimport java.util.*;
31113229Smdoddimport javax.tools.*;
32113229Smdodd
33113229Smdoddpublic class T5070898
34113229Smdodd{
35113229Smdodd    public static void main(String... args) throws Exception {
36115393Sru        new T5070898().run();
37113229Smdodd    }
38115393Sru
39255765Sdes    public void run() throws Exception {
40113229Smdodd        writeFile();
41255765Sdes        compileFile();
42255765Sdes
43255765Sdes        int rc = runJavah();
44234851Sbapt        System.err.println("exit code: " + rc);
45255765Sdes        if (rc == 0)
46255765Sdes            throw new Exception("unexpected exit code: " + rc);
47255765Sdes    }
48255765Sdes
49255765Sdes    void writeFile() throws Exception {
50255765Sdes        String content =
51255765Sdes            "package test;\n" +
52255765Sdes            "public class JavahTest{\n" +
53255765Sdes            "    public static void main(String args){\n" +
54255765Sdes            "        System.out.println(\"Test Message\");" +
55255765Sdes            "    }\n" +
56255765Sdes            "    private static native Object nativeTest();\n" +
57255765Sdes            "}\n";
58255765Sdes        FileWriter out = new FileWriter("JavahTest.java");
59255765Sdes        try {
60255765Sdes            out.write(content);
61255765Sdes        } finally {
62255765Sdes            out.close();
63255765Sdes        }
64255765Sdes    }
65255765Sdes
66255765Sdes    void compileFile() throws Exception {
67234851Sbapt        JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
68234851Sbapt        int rc = javac.run(null, null, null, "JavahTest.java");
69234851Sbapt        if (rc != 0)
70234851Sbapt            throw new Exception("compilation failed");
71255765Sdes    }
72255765Sdes
73234851Sbapt    int runJavah() throws Exception {
74255765Sdes        List<String> cmd = new ArrayList<String>();
75234851Sbapt        File java_home = new File(System.getProperty("java.home"));
76255765Sdes        cmd.add(new File(new File(java_home, "bin"), "javah").getPath());
77234851Sbapt
78234851Sbapt        cmd.add("JavahTest");
79255765Sdes
80255765Sdes        ProcessBuilder pb = new ProcessBuilder(cmd);
81255765Sdes        pb.redirectErrorStream(true);
82234851Sbapt        pb.environment().remove("CLASSPATH");
83255765Sdes        Process p = pb.start();
84255765Sdes        p.getOutputStream().close();
85255765Sdes
86255765Sdes        String line;
87129638Smdodd        DataInputStream in = new DataInputStream(p.getInputStream());
88130582Sru        try {
89129638Smdodd        while ((line = in.readLine()) != null)
90255765Sdes            System.err.println(line);
91129638Smdodd        } finally {
92115393Sru            in.close();
93255765Sdes        }
94255765Sdes
95130103Sceri        return p.waitFor();
96129638Smdodd    }
97129638Smdodd}
98129638Smdodd