duke@1: /* ohair@554: * Copyright (c) 2006, 2007, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: /* duke@1: * @test duke@1: * @bug 6400872 duke@1: * @summary REGRESSION: Java Compiler cannot find jar files referenced by other duke@1: * @run main T6400872 duke@1: */ duke@1: duke@1: // ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java duke@1: // ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class duke@1: // ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class duke@1: // ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java duke@1: duke@1: import java.io.*; duke@1: import java.nio.*; duke@1: import java.util.*; duke@1: import java.util.jar.*; duke@1: import javax.tools.*; duke@1: import javax.tools.StandardJavaFileManager.*; duke@1: duke@1: public class T6400872 { duke@1: static File testSrc = new File(System.getProperty("test.src", ".")); duke@1: static File testClasses = new File(System.getProperty("test.classes", ".")); duke@1: duke@1: public static void main(String... args) throws Exception { duke@1: // compile A.java and B.java duke@1: compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java")); duke@1: // put them in mutually referential class files duke@1: jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class")); duke@1: jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class")); duke@1: // verify we can successfully use the class path entries in the jar files duke@1: compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java")); duke@1: } duke@1: duke@1: static void compile(File classOutDir, Iterable classPath, File... files) duke@1: throws IOException { duke@1: System.err.println("compile..."); duke@1: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); duke@1: StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); duke@1: try { duke@1: Iterable fileObjects = duke@1: fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); duke@1: duke@1: List options = new ArrayList(); duke@1: if (classOutDir != null) { duke@1: options.add("-d"); duke@1: options.add(classOutDir.getPath()); duke@1: } duke@1: if (classPath != null) { duke@1: options.add("-classpath"); duke@1: options.add(join(classPath, File.pathSeparator)); duke@1: } duke@1: options.add("-verbose"); duke@1: duke@1: JavaCompiler.CompilationTask task = duke@1: compiler.getTask(null, fm, null, options, null, fileObjects); duke@1: if (!task.call()) duke@1: throw new AssertionError("compilation failed"); duke@1: } finally { duke@1: fm.close(); duke@1: } duke@1: } duke@1: duke@1: static void jar(File jar, Iterable classPath, File base, File... files) duke@1: throws IOException { duke@1: System.err.println("jar..."); duke@1: Manifest m = new Manifest(); duke@1: if (classPath != null) { duke@1: Attributes mainAttrs = m.getMainAttributes(); duke@1: mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); duke@1: mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); duke@1: } duke@1: OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); duke@1: JarOutputStream j = new JarOutputStream(out, m); duke@1: add(j, base, files); duke@1: j.close(); duke@1: } duke@1: duke@1: static void add(JarOutputStream j, File base, File... files) throws IOException { duke@1: if (files == null) duke@1: return; duke@1: duke@1: for (File f: files) duke@1: add(j, base, f); duke@1: } duke@1: duke@1: static void add(JarOutputStream j, File base, File file) throws IOException { duke@1: File f = new File(base, file.getPath()); duke@1: if (f.isDirectory()) { duke@1: String[] children = f.list(); duke@1: if (children != null) duke@1: for (String c: children) duke@1: add(j, base, new File(file, c)); duke@1: } else { duke@1: JarEntry e = new JarEntry(file.getPath()); duke@1: e.setSize(f.length()); duke@1: j.putNextEntry(e); duke@1: j.write(read(f)); duke@1: j.closeEntry(); duke@1: } duke@1: duke@1: } duke@1: duke@1: static byte[] read(File f) throws IOException { duke@1: byte[] buf = new byte[(int) f.length()]; duke@1: BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); duke@1: int offset = 0; duke@1: while (offset < buf.length) { duke@1: int n = in.read(buf, offset, buf.length - offset); duke@1: if (n < 0) duke@1: throw new EOFException(); duke@1: offset += n; duke@1: } duke@1: return buf; duke@1: } duke@1: duke@1: static Iterable iterable(T single) { duke@1: return Collections.singleton(single); duke@1: } duke@1: duke@1: static String join(Iterable iter, String sep) { duke@1: StringBuilder p = new StringBuilder(); duke@1: for (T t: iter) { duke@1: if (p.length() > 0) duke@1: p.append(' '); duke@1: p.append(t); duke@1: } duke@1: return p.toString(); duke@1: } duke@1: }