aoqi@0: /* aoqi@0: * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6725230 aoqi@0: * @summary Test to make sure that java Compilation with JSR199 does not ignore aoqi@0: * Class-Path in manifest aoqi@0: * @author vicente.romero aoqi@0: * @build TestCompileJARInClassPath aoqi@0: * @run main TestCompileJARInClassPath aoqi@0: */ aoqi@0: aoqi@0: import java.io.File; aoqi@0: import java.io.FileOutputStream; aoqi@0: import java.io.IOException; aoqi@0: import java.io.PrintStream; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import javax.tools.DiagnosticCollector; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.StandardLocation; aoqi@0: import javax.tools.ToolProvider; aoqi@0: aoqi@0: public class TestCompileJARInClassPath { aoqi@0: aoqi@0: public static void main(String args[]) throws Exception { aoqi@0: TestCompileJARInClassPath theTest = new TestCompileJARInClassPath(); aoqi@0: theTest.run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: try { aoqi@0: clean(); aoqi@0: generateFilesNeeded(); aoqi@0: compileWithJSR199(); aoqi@0: } finally { aoqi@0: clean(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void writeFile(String f, String contents) throws IOException { aoqi@0: PrintStream s = new PrintStream(new FileOutputStream(f)); aoqi@0: s.println(contents); aoqi@0: s.close(); aoqi@0: } aoqi@0: aoqi@0: void rm(String filename) throws Exception { aoqi@0: File f = new File(filename); aoqi@0: f.delete(); aoqi@0: if (f.exists()) aoqi@0: throw new Exception(filename + ": couldn't remove"); aoqi@0: } aoqi@0: aoqi@0: void clean() throws Exception { aoqi@0: rm("C1.java"); aoqi@0: rm("C1.class"); aoqi@0: rm("C1.jar"); aoqi@0: aoqi@0: rm("C2.java"); aoqi@0: rm("C2.class"); aoqi@0: rm("C2.jar"); aoqi@0: rm("MANIFEST.MF"); aoqi@0: aoqi@0: rm("C3.java"); aoqi@0: rm("C3.class"); aoqi@0: } aoqi@0: aoqi@0: void generateFilesNeeded() throws Exception { aoqi@0: sun.tools.jar.Main jarGenerator = new sun.tools.jar.Main(System.out, System.err, "jar"); aoqi@0: aoqi@0: writeFile("C1.java", aoqi@0: "public class C1 {public static void f() {}}"); aoqi@0: com.sun.tools.javac.Main.compile(new String[]{"C1.java"}); aoqi@0: jarGenerator.run(new String[] {"cf", "C1.jar", "C1.class"}); aoqi@0: aoqi@0: writeFile("C2.java", aoqi@0: "public class C2 {public static void g() {}}"); aoqi@0: writeFile("MANIFEST.MF", aoqi@0: "Manifest-Version: 1.0\n" + aoqi@0: "Class-Path: C1.jar\n" + aoqi@0: "Main-Class: C2"); aoqi@0: com.sun.tools.javac.Main.compile(new String[]{"C2.java"}); aoqi@0: jarGenerator.run(new String[] {"cfm", "C2.jar", "MANIFEST.MF", "C2.class"}); aoqi@0: aoqi@0: writeFile("C3.java", aoqi@0: "public class C3 {public static void h() {C2.g(); C1.f();}}"); aoqi@0: } aoqi@0: aoqi@0: void compileWithJSR199() throws IOException { aoqi@0: String cpath = "C2.jar"; aoqi@0: File clientJarFile = new File(cpath); aoqi@0: File sourceFileToCompile = new File("C3.java"); aoqi@0: aoqi@0: aoqi@0: javax.tools.JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); aoqi@0: DiagnosticCollector diagnostics = new DiagnosticCollector<>(); aoqi@0: StandardJavaFileManager stdFileManager = javac.getStandardFileManager(diagnostics, null, null); aoqi@0: aoqi@0: List files = new ArrayList<>(); aoqi@0: files.add(clientJarFile); aoqi@0: aoqi@0: stdFileManager.setLocation(StandardLocation.CLASS_PATH, files); aoqi@0: aoqi@0: Iterable sourceFiles = stdFileManager.getJavaFileObjects(sourceFileToCompile); aoqi@0: aoqi@0: if (!javac.getTask(null, stdFileManager, diagnostics, null, null, sourceFiles).call()) { aoqi@0: throw new AssertionError("compilation failed"); aoqi@0: } aoqi@0: } aoqi@0: }