/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ import jdk.testlibrary.OutputAnalyzer; import jdk.testlibrary.ProcessTools; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * @test * @bug 8076264 * @summary Launching app shouldn't require enclosing class for the main class. * @library /lib/testlibrary * @run main/othervm MainClassWithoutEnclosingClass */ public final class MainClassWithoutEnclosingClass { public static final class Main { public static void main(final String[] args) { System.out.print("Main"); } } static final String MAIN_CLASS = "MainClassWithoutEnclosingClass$Main"; public static void main(final String[] args) throws Exception { File zipFile = new File("MainClassWithoutEnclosingClass-tmp.zip"); zipFile.delete(); try (FileOutputStream fos = new FileOutputStream(zipFile); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos)) { // Add entries to allow the zip file to be used with "java -jar" zos.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF")); for (final String line : new String[]{ "Manifest-Version: 1.0", "Main-Class: " + MAIN_CLASS, }) { zos.write((line + "\n").getBytes("US-ASCII")); } zos.closeEntry(); String testClasses = System.getProperty("test.classes"); String baseName = MAIN_CLASS + ".class"; zos.putNextEntry(new ZipEntry(baseName)); Files.copy(Paths.get(testClasses, baseName), zos); zos.closeEntry(); } // Check java -jar String javaHome = System.getProperty("java.home"); String java = Paths.get(javaHome, "bin", "java").toString(); String[] cmd = {java, "-jar", zipFile.getName()}; ProcessBuilder pb = new ProcessBuilder(cmd); OutputAnalyzer a = ProcessTools.executeProcess(pb); a.shouldHaveExitValue(0); a.stdoutShouldMatch("\\AMain\\Z"); a.stderrShouldMatch("\\A\\Z"); zipFile.delete(); } }