jjg@14: /* jjg@14: * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. jjg@14: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@14: * jjg@14: * This code is free software; you can redistribute it and/or modify it jjg@14: * under the terms of the GNU General Public License version 2 only, as jjg@14: * published by the Free Software Foundation. jjg@14: * jjg@14: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@14: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@14: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@14: * version 2 for more details (a copy is included in the LICENSE file that jjg@14: * accompanied this code). jjg@14: * jjg@14: * You should have received a copy of the GNU General Public License version jjg@14: * 2 along with this work; if not, write to the Free Software Foundation, jjg@14: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@14: * jjg@14: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, jjg@14: * CA 95054 USA or visit www.sun.com if you need additional information or jjg@14: * have any questions. jjg@14: */ jjg@14: jjg@14: /* jjg@14: * @test jjg@14: * @bug 6638501 jjg@14: * @summary REGRESSION: Java Compiler cannot find jar files referenced by other jjg@14: * @run main JarFromManifestFailure jjg@14: */ jjg@14: jjg@14: import java.io.*; jjg@14: import java.nio.*; jjg@14: import java.util.*; jjg@14: import java.util.jar.*; jjg@14: import javax.tools.*; jjg@14: import javax.tools.StandardJavaFileManager.*; jjg@14: jjg@14: public class JarFromManifestFailure { jjg@14: static File testSrc = new File(System.getProperty("test.src", ".")); jjg@14: static File testClasses = new File(System.getProperty("test.classes", ".")); jjg@14: jjg@14: public static void main(String... args) throws Exception { jjg@14: compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java")); jjg@14: File libFile = new File(testClasses, "lib"); jjg@14: libFile.mkdir(); jjg@14: jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test")); jjg@14: jjg@14: ArrayList arList = new ArrayList(); jjg@14: arList.add(new File("HelloLib.jar")); jjg@14: jar(new File(libFile, "JarPointer.jar"), arList, testClasses); jjg@14: jjg@14: String[] args1 = { jjg@14: "-d", ".", jjg@14: "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), jjg@14: new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/') jjg@14: }; jjg@14: System.err.println("First compile!!!"); jjg@14: if (com.sun.tools.javac.Main.compile(args1) != 0) { jjg@14: throw new AssertionError("Failure in first compile!"); jjg@14: } jjg@14: jjg@14: System.err.println("Second compile!!!"); jjg@14: jjg@14: args1 = new String[] { jjg@14: "-d", ".", jjg@14: "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), jjg@14: new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/') jjg@14: }; jjg@14: if (com.sun.tools.javac.Main.compile(args1) != 0) { jjg@14: throw new AssertionError("Failure in second compile!"); jjg@14: } jjg@14: } jjg@14: jjg@14: static void compile(File classOutDir, Iterable classPath, File... files) { jjg@14: System.err.println("compile..."); jjg@14: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); jjg@14: StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); jjg@14: Iterable fileObjects = jjg@14: fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); jjg@14: jjg@14: List options = new ArrayList(); jjg@14: if (classOutDir != null) { jjg@14: options.add("-d"); jjg@14: options.add(classOutDir.getPath()); jjg@14: } jjg@14: if (classPath != null) { jjg@14: options.add("-classpath"); jjg@14: options.add(join(classPath, File.pathSeparator)); jjg@14: } jjg@14: options.add("-verbose"); jjg@14: jjg@14: JavaCompiler.CompilationTask task = jjg@14: compiler.getTask(null, fm, null, options, null, fileObjects); jjg@14: if (!task.call()) jjg@14: throw new AssertionError("compilation failed"); jjg@14: } jjg@14: jjg@14: static void jar(File jar, Iterable classPath, File base, File... files) jjg@14: throws IOException { jjg@14: System.err.println("jar..."); jjg@14: Manifest m = new Manifest(); jjg@14: if (classPath != null) { jjg@14: Attributes mainAttrs = m.getMainAttributes(); jjg@14: mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); jjg@14: mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); jjg@14: } jjg@14: OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); jjg@14: JarOutputStream j = new JarOutputStream(out, m); jjg@14: add(j, base, files); jjg@14: j.close(); jjg@14: } jjg@14: jjg@14: static void add(JarOutputStream j, File base, File... files) throws IOException { jjg@14: if (files == null) jjg@14: return; jjg@14: jjg@14: for (File f: files) jjg@14: add(j, base, f); jjg@14: } jjg@14: jjg@14: static void add(JarOutputStream j, File base, File file) throws IOException { jjg@14: File f = new File(base, file.getPath()); jjg@14: if (f.isDirectory()) { jjg@14: JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/')); jjg@14: e.setSize(file.length()); jjg@14: j.putNextEntry(e); jjg@14: String[] children = f.list(); jjg@14: if (children != null) { jjg@14: for (String c: children) { jjg@14: add(j, base, new File(file, c)); jjg@14: } jjg@14: } jjg@14: } else { jjg@14: JarEntry e = new JarEntry(file.getPath().replace('\\', '/')); jjg@14: e.setSize(f.length()); jjg@14: j.putNextEntry(e); jjg@14: j.write(read(f)); jjg@14: j.closeEntry(); jjg@14: } jjg@14: jjg@14: } jjg@14: jjg@14: static byte[] read(File f) throws IOException { jjg@14: byte[] buf = new byte[(int) f.length()]; jjg@14: BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); jjg@14: int offset = 0; jjg@14: while (offset < buf.length) { jjg@14: int n = in.read(buf, offset, buf.length - offset); jjg@14: if (n < 0) jjg@14: throw new EOFException(); jjg@14: offset += n; jjg@14: } jjg@14: return buf; jjg@14: } jjg@14: jjg@14: static Iterable iterable(T single) { jjg@14: return Collections.singleton(single); jjg@14: } jjg@14: jjg@14: static String join(Iterable iter, String sep) { jjg@14: StringBuilder p = new StringBuilder(); jjg@14: for (T t: iter) { jjg@14: if (p.length() > 0) jjg@14: p.append(' '); jjg@14: p.append(t); jjg@14: } jjg@14: return p.toString(); jjg@14: } jjg@14: }