test/tools/javah/T5070898.java

Fri, 23 Aug 2013 14:17:49 -0700

author
lana
date
Fri, 23 Aug 2013 14:17:49 -0700
changeset 1967
375834b5cf08
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Merge

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

mercurial