|
1 /* |
|
2 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6638501 |
|
27 * @summary REGRESSION: Java Compiler cannot find jar files referenced by other |
|
28 * @run main JarFromManifestFailure |
|
29 */ |
|
30 |
|
31 import java.io.*; |
|
32 import java.nio.*; |
|
33 import java.util.*; |
|
34 import java.util.jar.*; |
|
35 import javax.tools.*; |
|
36 import javax.tools.StandardJavaFileManager.*; |
|
37 |
|
38 public class JarFromManifestFailure { |
|
39 static File testSrc = new File(System.getProperty("test.src", ".")); |
|
40 static File testClasses = new File(System.getProperty("test.classes", ".")); |
|
41 |
|
42 public static void main(String... args) throws Exception { |
|
43 compile(testClasses, null, new File(testSrc, "HelloLib/test/HelloImpl.java"), new File(testSrc, "WsCompileExample.java")); |
|
44 File libFile = new File(testClasses, "lib"); |
|
45 libFile.mkdir(); |
|
46 jar(new File(libFile, "HelloLib.jar"), new ArrayList(), testClasses, new File("test")); |
|
47 |
|
48 ArrayList arList = new ArrayList(); |
|
49 arList.add(new File("HelloLib.jar")); |
|
50 jar(new File(libFile, "JarPointer.jar"), arList, testClasses); |
|
51 |
|
52 String[] args1 = { |
|
53 "-d", ".", |
|
54 "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), |
|
55 new File(testSrc, "test/SayHello.java").getPath().replace('\\', '/') |
|
56 }; |
|
57 System.err.println("First compile!!!"); |
|
58 if (com.sun.tools.javac.Main.compile(args1) != 0) { |
|
59 throw new AssertionError("Failure in first compile!"); |
|
60 } |
|
61 |
|
62 System.err.println("Second compile!!!"); |
|
63 |
|
64 args1 = new String[] { |
|
65 "-d", ".", |
|
66 "-cp", new File(libFile, "JarPointer.jar").getPath().replace('\\', '/'), |
|
67 new File(testSrc, "test1/SayHelloToo.java").getPath().replace('\\', '/') |
|
68 }; |
|
69 if (com.sun.tools.javac.Main.compile(args1) != 0) { |
|
70 throw new AssertionError("Failure in second compile!"); |
|
71 } |
|
72 } |
|
73 |
|
74 static void compile(File classOutDir, Iterable<File> classPath, File... files) { |
|
75 System.err.println("compile..."); |
|
76 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); |
|
77 StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null); |
|
78 Iterable<? extends JavaFileObject> fileObjects = |
|
79 fm.getJavaFileObjectsFromFiles(Arrays.asList(files)); |
|
80 |
|
81 List<String> options = new ArrayList<String>(); |
|
82 if (classOutDir != null) { |
|
83 options.add("-d"); |
|
84 options.add(classOutDir.getPath()); |
|
85 } |
|
86 if (classPath != null) { |
|
87 options.add("-classpath"); |
|
88 options.add(join(classPath, File.pathSeparator)); |
|
89 } |
|
90 options.add("-verbose"); |
|
91 |
|
92 JavaCompiler.CompilationTask task = |
|
93 compiler.getTask(null, fm, null, options, null, fileObjects); |
|
94 if (!task.call()) |
|
95 throw new AssertionError("compilation failed"); |
|
96 } |
|
97 |
|
98 static void jar(File jar, Iterable<File> classPath, File base, File... files) |
|
99 throws IOException { |
|
100 System.err.println("jar..."); |
|
101 Manifest m = new Manifest(); |
|
102 if (classPath != null) { |
|
103 Attributes mainAttrs = m.getMainAttributes(); |
|
104 mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); |
|
105 mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " ")); |
|
106 } |
|
107 OutputStream out = new BufferedOutputStream(new FileOutputStream(jar)); |
|
108 JarOutputStream j = new JarOutputStream(out, m); |
|
109 add(j, base, files); |
|
110 j.close(); |
|
111 } |
|
112 |
|
113 static void add(JarOutputStream j, File base, File... files) throws IOException { |
|
114 if (files == null) |
|
115 return; |
|
116 |
|
117 for (File f: files) |
|
118 add(j, base, f); |
|
119 } |
|
120 |
|
121 static void add(JarOutputStream j, File base, File file) throws IOException { |
|
122 File f = new File(base, file.getPath()); |
|
123 if (f.isDirectory()) { |
|
124 JarEntry e = new JarEntry(new String(file.getPath() + File.separator).replace('\\', '/')); |
|
125 e.setSize(file.length()); |
|
126 j.putNextEntry(e); |
|
127 String[] children = f.list(); |
|
128 if (children != null) { |
|
129 for (String c: children) { |
|
130 add(j, base, new File(file, c)); |
|
131 } |
|
132 } |
|
133 } else { |
|
134 JarEntry e = new JarEntry(file.getPath().replace('\\', '/')); |
|
135 e.setSize(f.length()); |
|
136 j.putNextEntry(e); |
|
137 j.write(read(f)); |
|
138 j.closeEntry(); |
|
139 } |
|
140 |
|
141 } |
|
142 |
|
143 static byte[] read(File f) throws IOException { |
|
144 byte[] buf = new byte[(int) f.length()]; |
|
145 BufferedInputStream in = new BufferedInputStream(new FileInputStream(f)); |
|
146 int offset = 0; |
|
147 while (offset < buf.length) { |
|
148 int n = in.read(buf, offset, buf.length - offset); |
|
149 if (n < 0) |
|
150 throw new EOFException(); |
|
151 offset += n; |
|
152 } |
|
153 return buf; |
|
154 } |
|
155 |
|
156 static <T> Iterable<T> iterable(T single) { |
|
157 return Collections.singleton(single); |
|
158 } |
|
159 |
|
160 static <T> String join(Iterable<T> iter, String sep) { |
|
161 StringBuilder p = new StringBuilder(); |
|
162 for (T t: iter) { |
|
163 if (p.length() > 0) |
|
164 p.append(' '); |
|
165 p.append(t); |
|
166 } |
|
167 return p.toString(); |
|
168 } |
|
169 } |