371N/A/*
553N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
371N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
371N/A *
371N/A * This code is free software; you can redistribute it and/or modify it
371N/A * under the terms of the GNU General Public License version 2 only, as
371N/A * published by the Free Software Foundation.
371N/A *
371N/A * This code is distributed in the hope that it will be useful, but WITHOUT
371N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
371N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
371N/A * version 2 for more details (a copy is included in the LICENSE file that
371N/A * accompanied this code).
371N/A *
371N/A * You should have received a copy of the GNU General Public License version
371N/A * 2 along with this work; if not, write to the Free Software Foundation,
371N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
371N/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.
371N/A */
371N/A
371N/A/*
371N/A * @test
371N/A * @run main/othervm -Xmx512m -Xms512m T6558476
371N/A */
371N/A
371N/Aimport java.io.File;
371N/Aimport java.io.FileInputStream;
371N/Aimport java.io.FileOutputStream;
371N/Aimport java.io.IOException;
371N/Aimport java.util.Random;
371N/A
371N/Aimport com.sun.tools.javac.Main;
371N/A
371N/Apublic class T6558476 {
371N/A private static File copyFileTo(File file, File directory) throws IOException {
371N/A File newFile = new File(directory, file.getName());
371N/A FileInputStream fis = null;
371N/A FileOutputStream fos = null;
371N/A try {
371N/A fis = new FileInputStream(file);
371N/A fos = new FileOutputStream(newFile);
371N/A byte buff[] = new byte[1024];
371N/A int val;
371N/A while ((val = fis.read(buff)) > 0)
371N/A fos.write(buff, 0, val);
371N/A } finally {
371N/A if (fis != null)
371N/A fis.close();
371N/A if (fos != null)
371N/A fos.close();
371N/A }
371N/A return newFile;
371N/A }
371N/A
371N/A private static String generateJavaClass(String className) {
371N/A StringBuffer sb = new StringBuffer();
371N/A sb.append("import sun.net.spi.nameservice.dns.DNSNameService;\n");
371N/A sb.append("public class ");
371N/A sb.append(className);
371N/A sb.append(" {\n");
371N/A sb.append(" public void doStuff() {\n");
371N/A sb.append(" DNSNameService dns = null;\n");
371N/A sb.append(" }\n");
371N/A sb.append("}\n");
371N/A return sb.toString();
371N/A }
371N/A
371N/A public static void main(String[] args) throws IOException {
371N/A File javaHomeDir = new File(System.getProperty("java.home"));
371N/A File tmpDir = new File(System.getProperty("java.io.tmpdir"));
371N/A File outputDir = new File(tmpDir, "outputDir" + new Random().nextInt(65536));
371N/A outputDir.mkdir();
371N/A outputDir.deleteOnExit();
371N/A
371N/A File dnsjarfile = new File(javaHomeDir, "lib" + File.separator + "ext" + File.separator + "dnsns.jar");
371N/A File tmpJar = copyFileTo(dnsjarfile, outputDir);
371N/A String className = "TheJavaFile";
371N/A File javaFile = new File(outputDir, className + ".java");
371N/A javaFile.deleteOnExit();
371N/A FileOutputStream fos = new FileOutputStream(javaFile);
371N/A fos.write(generateJavaClass(className).getBytes());
371N/A fos.close();
371N/A
371N/A int rc = Main.compile(new String[]{"-d", outputDir.getPath(),
371N/A "-classpath",
371N/A tmpJar.getPath(),
371N/A javaFile.getAbsolutePath()});
371N/A if (rc != 0) {
371N/A throw new Error("Couldn't compile the file (exit code=" + rc + ")");
371N/A }
371N/A
371N/A if (tmpJar.delete()) {
371N/A System.out.println("jar file successfully deleted");
371N/A } else {
371N/A throw new Error("Error deleting file \"" + tmpJar.getPath() + "\"");
371N/A }
371N/A }
371N/A}