Path: blob/master/test/langtools/tools/javac/6400872/T6400872.java
41149 views
/*1* Copyright (c) 2006, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 640087226* @summary REGRESSION: Java Compiler cannot find jar files referenced by other27* @modules java.compiler28* jdk.compiler29* @run main T640087230*/3132// ${TESTJAVA}/bin/javac -d ${TESTCLASSES} ${TESTSRC}/A.java ${TESTSRC}/B.java33// ${TESTJAVA}/bin/jar -cfm A.jar ${TESTSRC}/A/META-INF/MANIFEST.MF -C ${TESTCLASSES} A.class34// ${TESTJAVA}/bin/jar -cfm B.jar ${TESTSRC}/B/META-INF/MANIFEST.MF -C ${TESTCLASSES} B.class35// ${TESTJAVA}/bin/javac -cp A.jar ${TESTSRC}/C.java3637import java.io.*;38import java.nio.*;39import java.util.*;40import java.util.jar.*;41import javax.tools.*;42import javax.tools.StandardJavaFileManager.*;4344public class T6400872 {45static File testSrc = new File(System.getProperty("test.src", "."));46static File testClasses = new File(System.getProperty("test.classes", "."));4748public static void main(String... args) throws Exception {49// compile A.java and B.java50compile(testClasses, null, new File(testSrc, "A.java"), new File(testSrc, "B.java"));51// put them in mutually referential class files52jar(new File("A.jar"), iterable(new File(".", "B.jar")), testClasses, new File("A.class"));53jar(new File("B.jar"), iterable(new File(".", "A.jar")), testClasses, new File("B.class"));54// verify we can successfully use the class path entries in the jar files55compile(new File("."), iterable(new File("A.jar")), new File(testSrc, "C.java"));56}5758static void compile(File classOutDir, Iterable<File> classPath, File... files)59throws IOException {60System.err.println("compile...");61JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();62try (StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null)) {63Iterable<? extends JavaFileObject> fileObjects =64fm.getJavaFileObjectsFromFiles(Arrays.asList(files));6566List<String> options = new ArrayList<String>();67if (classOutDir != null) {68options.add("-d");69options.add(classOutDir.getPath());70}71if (classPath != null) {72options.add("-classpath");73options.add(join(classPath, File.pathSeparator));74}75options.add("-verbose");7677JavaCompiler.CompilationTask task =78compiler.getTask(null, fm, null, options, null, fileObjects);79if (!task.call())80throw new AssertionError("compilation failed");81}82}8384static void jar(File jar, Iterable<File> classPath, File base, File... files)85throws IOException {86System.err.println("jar...");87Manifest m = new Manifest();88if (classPath != null) {89Attributes mainAttrs = m.getMainAttributes();90mainAttrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");91mainAttrs.put(Attributes.Name.CLASS_PATH, join(classPath, " "));92}93OutputStream out = new BufferedOutputStream(new FileOutputStream(jar));94JarOutputStream j = new JarOutputStream(out, m);95add(j, base, files);96j.close();97}9899static void add(JarOutputStream j, File base, File... files) throws IOException {100if (files == null)101return;102103for (File f: files)104add(j, base, f);105}106107static void add(JarOutputStream j, File base, File file) throws IOException {108File f = new File(base, file.getPath());109if (f.isDirectory()) {110String[] children = f.list();111if (children != null)112for (String c: children)113add(j, base, new File(file, c));114} else {115JarEntry e = new JarEntry(file.getPath());116e.setSize(f.length());117j.putNextEntry(e);118j.write(read(f));119j.closeEntry();120}121122}123124static byte[] read(File f) throws IOException {125byte[] buf = new byte[(int) f.length()];126BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));127int offset = 0;128while (offset < buf.length) {129int n = in.read(buf, offset, buf.length - offset);130if (n < 0)131throw new EOFException();132offset += n;133}134return buf;135}136137static <T> Iterable<T> iterable(T single) {138return Collections.singleton(single);139}140141static <T> String join(Iterable<T> iter, String sep) {142StringBuilder p = new StringBuilder();143for (T t: iter) {144if (p.length() > 0)145p.append(' ');146p.append(t);147}148return p.toString();149}150}151152153