1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/Paths/6638501/JarFromManifestFailure.java Fri Mar 14 16:09:30 2008 -0700 1.3 @@ -0,0 +1,169 @@ 1.4 +/* 1.5 + * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 6638501 1.30 + * @summary REGRESSION: Java Compiler cannot find jar files referenced by other 1.31 + * @run main JarFromManifestFailure 1.32 + */ 1.33 + 1.34 +import java.io.*; 1.35 +import java.nio.*; 1.36 +import java.util.*; 1.37 +import java.util.jar.*; 1.38 +import javax.tools.*; 1.39 +import javax.tools.StandardJavaFileManager.*; 1.40 + 1.41 +public class JarFromManifestFailure { 1.42 + static File testSrc = new File(System.getProperty("test.src", ".")); 1.43 + static File testClasses = new File(System.getProperty("test.classes", ".")); 1.44 + 1.45 + public static void main(String... args) throws Exception { 1.46 + compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java")); 1.47 + File libFile = new File(testClasses, "lib"); 1.48 + libFile.mkdir(); 1.49 + jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test")); 1.50 + 1.51 + ArrayList arList = new ArrayList(); 1.52 + arList.add(new File("HelloLib.jar")); 1.53 + jar(new File(libFile, "JarPointer.jar"), arList, testClasses); 1.54 + 1.55 + String[] args1 = { 1.56 + "-d", ".", 1.57 + "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), 1.58 + new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/') 1.59 + }; 1.60 + System.err.println("First compile!!!"); 1.61 + if (com.sun.tools.javac.Main.compile(args1) != 0) { 1.62 + throw new AssertionError("Failure in first compile!"); 1.63 + } 1.64 + 1.65 + System.err.println("Second compile!!!"); 1.66 + 1.67 + args1 = new String[] { 1.68 + "-d", ".", 1.69 + "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), 1.70 + new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/') 1.71 + }; 1.72 + if (com.sun.tools.javac.Main.compile(args1) != 0) { 1.73 + throw new AssertionError("Failure in second compile!"); 1.74 + } 1.75 + } 1.76 + 1.77 + static void compile(File classOutDir, Iterable<File> classPath, File... files) { 1.78 + System.err.println("compile..."); 1.79 + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); 1.80 + StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); 1.81 + Iterable<? extends JavaFileObject> fileObjects = 1.82 + fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); 1.83 + 1.84 + List<String> options = new ArrayList<String>(); 1.85 + if (classOutDir != null) { 1.86 + options.add("-d"); 1.87 + options.add(classOutDir.getPath()); 1.88 + } 1.89 + if (classPath != null) { 1.90 + options.add("-classpath"); 1.91 + options.add(join(classPath, File.pathSeparator)); 1.92 + } 1.93 + options.add("-verbose"); 1.94 + 1.95 + JavaCompiler.CompilationTask task = 1.96 + compiler.getTask(null, fm, null, options, null, fileObjects); 1.97 + if (!task.call()) 1.98 + throw new AssertionError("compilation failed"); 1.99 + } 1.100 + 1.101 + static void jar(File jar, Iterable<File> classPath, File base, File... files) 1.102 + throws IOException { 1.103 + System.err.println("jar..."); 1.104 + Manifest m = new Manifest(); 1.105 + if (classPath != null) { 1.106 + Attributes mainAttrs = m.getMainAttributes(); 1.107 + mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); 1.108 + mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); 1.109 + } 1.110 + OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); 1.111 + JarOutputStream j = new JarOutputStream(out, m); 1.112 + add(j, base, files); 1.113 + j.close(); 1.114 + } 1.115 + 1.116 + static void add(JarOutputStream j, File base, File... files) throws IOException { 1.117 + if (files == null) 1.118 + return; 1.119 + 1.120 + for (File f: files) 1.121 + add(j, base, f); 1.122 + } 1.123 + 1.124 + static void add(JarOutputStream j, File base, File file) throws IOException { 1.125 + File f = new File(base, file.getPath()); 1.126 + if (f.isDirectory()) { 1.127 + JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/')); 1.128 + e.setSize(file.length()); 1.129 + j.putNextEntry(e); 1.130 + String[] children = f.list(); 1.131 + if (children != null) { 1.132 + for (String c: children) { 1.133 + add(j, base, new File(file, c)); 1.134 + } 1.135 + } 1.136 + } else { 1.137 + JarEntry e = new JarEntry(file.getPath().replace('\\', '/')); 1.138 + e.setSize(f.length()); 1.139 + j.putNextEntry(e); 1.140 + j.write(read(f)); 1.141 + j.closeEntry(); 1.142 + } 1.143 + 1.144 + } 1.145 + 1.146 + static byte[] read(File f) throws IOException { 1.147 + byte[] buf = new byte[(int) f.length()]; 1.148 + BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); 1.149 + int offset = 0; 1.150 + while (offset < buf.length) { 1.151 + int n = in.read(buf, offset, buf.length - offset); 1.152 + if (n < 0) 1.153 + throw new EOFException(); 1.154 + offset += n; 1.155 + } 1.156 + return buf; 1.157 + } 1.158 + 1.159 + static <T> Iterable<T> iterable(T single) { 1.160 + return Collections.singleton(single); 1.161 + } 1.162 + 1.163 + static <T> String join(Iterable<T> iter, String sep) { 1.164 + StringBuilder p = new StringBuilder(); 1.165 + for (T t: iter) { 1.166 + if (p.length() > 0) 1.167 + p.append(' '); 1.168 + p.append(t); 1.169 + } 1.170 + return p.toString(); 1.171 + } 1.172 +}