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