|
1 /* |
|
2 * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* @test |
|
25 * @bug 5070898 |
|
26 * @summary javah command doesn't throw correct exit code in case of error |
|
27 */ |
|
28 |
|
29 import java.io.*; |
|
30 import java.util.*; |
|
31 import javax.tools.*; |
|
32 |
|
33 public class T5070898 |
|
34 { |
|
35 public static void main(String... args) throws Exception { |
|
36 new T5070898().run(); |
|
37 } |
|
38 |
|
39 public void run() throws Exception { |
|
40 writeFile(); |
|
41 compileFile(); |
|
42 |
|
43 int rc = runJavah(); |
|
44 System.err.println("exit code: " + rc); |
|
45 if (rc == 0) |
|
46 throw new Exception("unexpected exit code: " + rc); |
|
47 } |
|
48 |
|
49 void writeFile() throws Exception { |
|
50 String content = |
|
51 "package test;\n" + |
|
52 "public class JavahTest{\n" + |
|
53 " public static void main(String args){\n" + |
|
54 " System.out.println(\"Test Message\");" + |
|
55 " }\n" + |
|
56 " private static native Object nativeTest();\n" + |
|
57 "}\n"; |
|
58 FileWriter out = new FileWriter("JavahTest.java"); |
|
59 try { |
|
60 out.write(content); |
|
61 } finally { |
|
62 out.close(); |
|
63 } |
|
64 } |
|
65 |
|
66 void compileFile() throws Exception { |
|
67 JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); |
|
68 int rc = javac.run(null, null, null, "JavahTest.java"); |
|
69 if (rc != 0) |
|
70 throw new Exception("compilation failed"); |
|
71 } |
|
72 |
|
73 int runJavah() throws Exception { |
|
74 List<String> cmd = new ArrayList<String>(); |
|
75 File java_home = new File(System.getProperty("java.home")); |
|
76 if (java_home.getName().equals("jre")) |
|
77 java_home = java_home.getParentFile(); |
|
78 cmd.add(new File(new File(java_home, "bin"), "javah").getPath()); |
|
79 |
|
80 // ensure we run with the same bootclasspath as this test, |
|
81 // in case this test is being run with -Xbootclasspath |
|
82 cmd.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path")); |
|
83 |
|
84 cmd.add("JavahTest"); |
|
85 |
|
86 ProcessBuilder pb = new ProcessBuilder(cmd); |
|
87 pb.redirectErrorStream(true); |
|
88 pb.environment().remove("CLASSPATH"); |
|
89 Process p = pb.start(); |
|
90 p.getOutputStream().close(); |
|
91 |
|
92 String line; |
|
93 DataInputStream in = new DataInputStream(p.getInputStream()); |
|
94 try { |
|
95 while ((line = in.readLine()) != null) |
|
96 System.err.println(line); |
|
97 } finally { |
|
98 in.close(); |
|
99 } |
|
100 |
|
101 return p.waitFor(); |
|
102 } |
|
103 } |