aoqi@0: /* aoqi@0: * Copyright (c) 2007, 2008, 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 6638501 aoqi@0: * @summary REGRESSION: Java Compiler cannot find jar files referenced by other aoqi@0: * @run main JarFromManifestFailure aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.nio.*; aoqi@0: import java.util.*; aoqi@0: import java.util.jar.*; aoqi@0: import javax.tools.*; aoqi@0: import javax.tools.StandardJavaFileManager.*; aoqi@0: aoqi@0: public class JarFromManifestFailure { aoqi@0: static File testSrc = new File(System.getProperty("test.src", ".")); aoqi@0: static File testClasses = new File(System.getProperty("test.classes", ".")); aoqi@0: aoqi@0: public static void main(String... args) throws Exception { aoqi@0: compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java")); aoqi@0: File libFile = new File(testClasses, "lib"); aoqi@0: libFile.mkdir(); aoqi@0: jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test")); aoqi@0: aoqi@0: ArrayList arList = new ArrayList(); aoqi@0: arList.add(new File("HelloLib.jar")); aoqi@0: jar(new File(libFile, "JarPointer.jar"), arList, testClasses); aoqi@0: aoqi@0: String[] args1 = { aoqi@0: "-d", ".", aoqi@0: "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), aoqi@0: new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/') aoqi@0: }; aoqi@0: System.err.println("First compile!!!"); aoqi@0: if (com.sun.tools.javac.Main.compile(args1) != 0) { aoqi@0: throw new AssertionError("Failure in first compile!"); aoqi@0: } aoqi@0: aoqi@0: System.err.println("Second compile!!!"); aoqi@0: aoqi@0: args1 = new String[] { aoqi@0: "-d", ".", aoqi@0: "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), aoqi@0: new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/') aoqi@0: }; aoqi@0: if (com.sun.tools.javac.Main.compile(args1) != 0) { aoqi@0: throw new AssertionError("Failure in second compile!"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: static void compile(File classOutDir, Iterable classPath, File... files) { aoqi@0: System.err.println("compile..."); aoqi@0: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); aoqi@0: StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); aoqi@0: Iterable fileObjects = aoqi@0: fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); aoqi@0: aoqi@0: List options = new ArrayList(); aoqi@0: if (classOutDir != null) { aoqi@0: options.add("-d"); aoqi@0: options.add(classOutDir.getPath()); aoqi@0: } aoqi@0: if (classPath != null) { aoqi@0: options.add("-classpath"); aoqi@0: options.add(join(classPath, File.pathSeparator)); aoqi@0: } aoqi@0: options.add("-verbose"); aoqi@0: aoqi@0: JavaCompiler.CompilationTask task = aoqi@0: compiler.getTask(null, fm, null, options, null, fileObjects); aoqi@0: if (!task.call()) aoqi@0: throw new AssertionError("compilation failed"); aoqi@0: } aoqi@0: aoqi@0: static void jar(File jar, Iterable classPath, File base, File... files) aoqi@0: throws IOException { aoqi@0: System.err.println("jar..."); aoqi@0: Manifest m = new Manifest(); aoqi@0: if (classPath != null) { aoqi@0: Attributes mainAttrs = m.getMainAttributes(); aoqi@0: mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); aoqi@0: mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); aoqi@0: } aoqi@0: OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); aoqi@0: JarOutputStream j = new JarOutputStream(out, m); aoqi@0: add(j, base, files); aoqi@0: j.close(); aoqi@0: } aoqi@0: aoqi@0: static void add(JarOutputStream j, File base, File... files) throws IOException { aoqi@0: if (files == null) aoqi@0: return; aoqi@0: aoqi@0: for (File f: files) aoqi@0: add(j, base, f); aoqi@0: } aoqi@0: aoqi@0: static void add(JarOutputStream j, File base, File file) throws IOException { aoqi@0: File f = new File(base, file.getPath()); aoqi@0: if (f.isDirectory()) { aoqi@0: JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/')); aoqi@0: e.setSize(file.length()); aoqi@0: j.putNextEntry(e); aoqi@0: String[] children = f.list(); aoqi@0: if (children != null) { aoqi@0: for (String c: children) { aoqi@0: add(j, base, new File(file, c)); aoqi@0: } aoqi@0: } aoqi@0: } else { aoqi@0: JarEntry e = new JarEntry(file.getPath().replace('\\', '/')); aoqi@0: e.setSize(f.length()); aoqi@0: j.putNextEntry(e); aoqi@0: j.write(read(f)); aoqi@0: j.closeEntry(); aoqi@0: } aoqi@0: aoqi@0: } aoqi@0: aoqi@0: static byte[] read(File f) throws IOException { aoqi@0: byte[] buf = new byte[(int) f.length()]; aoqi@0: BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); aoqi@0: int offset = 0; aoqi@0: while (offset < buf.length) { aoqi@0: int n = in.read(buf, offset, buf.length - offset); aoqi@0: if (n < 0) aoqi@0: throw new EOFException(); aoqi@0: offset += n; aoqi@0: } aoqi@0: return buf; aoqi@0: } aoqi@0: aoqi@0: static Iterable iterable(T single) { aoqi@0: return Collections.singleton(single); aoqi@0: } aoqi@0: aoqi@0: static String join(Iterable iter, String sep) { aoqi@0: StringBuilder p = new StringBuilder(); aoqi@0: for (T t: iter) { aoqi@0: if (p.length() > 0) aoqi@0: p.append(' '); aoqi@0: p.append(t); aoqi@0: } aoqi@0: return p.toString(); aoqi@0: } aoqi@0: }