Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javah/T5070898.java
32285 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 507089825* @summary javah command doesn't throw correct exit code in case of error26*/2728import java.io.*;29import java.util.*;30import javax.tools.*;3132public class T507089833{34public static void main(String... args) throws Exception {35new T5070898().run();36}3738public void run() throws Exception {39writeFile();40compileFile();4142int rc = runJavah();43System.err.println("exit code: " + rc);44if (rc == 0)45throw new Exception("unexpected exit code: " + rc);46}4748void writeFile() throws Exception {49String content =50"package test;\n" +51"public class JavahTest{\n" +52" public static void main(String args){\n" +53" System.out.println(\"Test Message\");" +54" }\n" +55" private static native Object nativeTest();\n" +56"}\n";57FileWriter out = new FileWriter("JavahTest.java");58try {59out.write(content);60} finally {61out.close();62}63}6465void compileFile() throws Exception {66JavaCompiler javac = ToolProvider.getSystemJavaCompiler();67int rc = javac.run(null, null, null, "JavahTest.java");68if (rc != 0)69throw new Exception("compilation failed");70}7172int runJavah() throws Exception {73List<String> cmd = new ArrayList<String>();74File java_home = new File(System.getProperty("java.home"));75if (java_home.getName().equals("jre"))76java_home = java_home.getParentFile();77cmd.add(new File(new File(java_home, "bin"), "javah").getPath());7879// ensure we run with the same bootclasspath as this test,80// in case this test is being run with -Xbootclasspath81cmd.add("-J-Xbootclasspath:" + System.getProperty("sun.boot.class.path"));8283cmd.add("JavahTest");8485ProcessBuilder pb = new ProcessBuilder(cmd);86pb.redirectErrorStream(true);87pb.environment().remove("CLASSPATH");88Process p = pb.start();89p.getOutputStream().close();9091String line;92DataInputStream in = new DataInputStream(p.getInputStream());93try {94while ((line = in.readLine()) != null)95System.err.println(line);96} finally {97in.close();98}99100return p.waitFor();101}102}103104105