1 /* 2 * Copyright (c) 2015, 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 import jdk.testlibrary.OutputAnalyzer; 25 import jdk.testlibrary.ProcessTools; 26 27 import java.io.BufferedOutputStream; 28 import java.io.File; 29 import java.io.FileOutputStream; 30 import java.nio.file.Files; 31 import java.nio.file.Paths; 32 import java.util.zip.ZipEntry; 33 import java.util.zip.ZipOutputStream; 34 35 /** 36 * @test 37 * @bug 8076264 38 * @summary Launching app shouldn't require enclosing class for the main class. 39 * @library /lib/testlibrary 40 * @run main/othervm MainClassWithoutEnclosingClass 41 */ 42 public final class MainClassWithoutEnclosingClass { 43 44 public static final class Main { 45 public static void main(final String[] args) { 46 System.out.print("Main"); 47 } 48 } 49 50 static final String MAIN_CLASS = "MainClassWithoutEnclosingClass$Main"; 51 52 public static void main(final String[] args) throws Exception { 53 File zipFile = new File("MainClassWithoutEnclosingClass-tmp.zip"); 54 zipFile.delete(); 55 56 try (FileOutputStream fos = new FileOutputStream(zipFile); 57 BufferedOutputStream bos = new BufferedOutputStream(fos); 58 ZipOutputStream zos = new ZipOutputStream(bos)) { 59 60 // Add entries to allow the zip file to be used with "java -jar" 61 zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); 62 for (final String line : new String[]{ 63 "Manifest-Version: 1.0", 64 "Main-Class: " + MAIN_CLASS, 65 }) { 66 zos.write((line + "\n").getBytes("US-ASCII")); 67 } 68 zos.closeEntry(); 69 70 String testClasses = System.getProperty("test.classes"); 71 String baseName = MAIN_CLASS + ".class"; 72 zos.putNextEntry(new ZipEntry(baseName)); 73 Files.copy(Paths.get(testClasses, baseName), zos); 74 zos.closeEntry(); 75 } 76 // Check java -jar 77 String javaHome = System.getProperty("java.home"); 78 String java = Paths.get(javaHome, "bin", "java").toString(); 79 String[] cmd = {java, "-jar", zipFile.getName()}; 80 ProcessBuilder pb = new ProcessBuilder(cmd); 81 OutputAnalyzer a = ProcessTools.executeProcess(pb); 82 a.shouldHaveExitValue(0); 83 a.stdoutShouldMatch("\\AMain\\Z"); 84 a.stderrShouldMatch("\\A\\Z"); 85 zipFile.delete(); 86 } 87 }