20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.nio.file.Path; 25 import java.io.InputStream; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 29 import jdk.jpackage.internal.IOUtils; 30 import jdk.jpackage.test.Test; 31 import jdk.jpackage.test.HelloApp; 32 import jdk.jpackage.test.JPackageCommand; 33 34 /* 35 * @test 36 * @summary jpackage with --win-console 37 * @library ../helpers 38 * @requires (os.family == "windows") 39 * @modules jdk.jpackage/jdk.jpackage.internal 40 * @run main/othervm -Xmx512m WinConsoleTest 41 */ 42 public class WinConsoleTest { 43 44 public static void main(String[] args) throws IOException { 45 JPackageCommand cmd = JPackageCommand.helloAppImage(); 46 final Path launcherPath = cmd.appImage().resolve( 47 cmd.launcherPathInAppImage()); 48 49 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 50 cmd.execute().assertExitCodeIsZero(); 51 HelloApp.executeAndVerifyOutput(launcherPath); 52 checkSubsystem(launcherPath, false); 53 54 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 55 cmd.addArgument("--win-console").execute().assertExitCodeIsZero(); 56 HelloApp.executeAndVerifyOutput(launcherPath); 57 checkSubsystem(launcherPath, true); 58 } 59 60 private static void checkSubsystem(Path path, boolean isConsole) throws 61 IOException { 62 final int subsystemGui = 2; 63 final int subsystemConsole = 3; 64 final int bufferSize = 512; 65 66 final int expectedSubsystem = isConsole ? subsystemConsole : subsystemGui; 67 68 try (InputStream inputStream = new FileInputStream(path.toString())) { 69 byte[] bytes = new byte[bufferSize]; 70 Test.assertEquals(bufferSize, inputStream.read(bytes), 71 String.format("Check %d bytes were read from %s file", 72 bufferSize, path)); 73 74 // Check PE header for console or Win GUI app. 75 // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers 76 for (int i = 0; i < (bytes.length - 4); i++) { 77 if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 | 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.nio.file.Path; 25 import java.io.InputStream; 26 import java.io.FileInputStream; 27 import java.io.IOException; 28 29 import jdk.jpackage.internal.IOUtils; 30 import jdk.jpackage.test.Test; 31 import jdk.jpackage.test.HelloApp; 32 import jdk.jpackage.test.JPackageCommand; 33 34 /* 35 * @test 36 * @summary jpackage with --win-console 37 * @library ../helpers 38 * @requires (os.family == "windows") 39 * @modules jdk.jpackage/jdk.jpackage.internal 40 * @run main/othervm/timeout=360 -Xmx512m WinConsoleTest 41 */ 42 public class WinConsoleTest { 43 44 public static void main(String[] args) { 45 Test.run(args, () -> { 46 JPackageCommand cmd = JPackageCommand.helloAppImage(); 47 final Path launcherPath = cmd.appImage().resolve( 48 cmd.launcherPathInAppImage()); 49 50 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 51 cmd.execute().assertExitCodeIsZero(); 52 HelloApp.executeLauncherAndVerifyOutput(cmd); 53 checkSubsystem(launcherPath, false); 54 55 IOUtils.deleteRecursive(cmd.outputDir().toFile()); 56 cmd.addArgument("--win-console").execute().assertExitCodeIsZero(); 57 HelloApp.executeLauncherAndVerifyOutput(cmd); 58 checkSubsystem(launcherPath, true); 59 }); 60 } 61 62 private static void checkSubsystem(Path path, boolean isConsole) throws 63 IOException { 64 final int subsystemGui = 2; 65 final int subsystemConsole = 3; 66 final int bufferSize = 512; 67 68 final int expectedSubsystem = isConsole ? subsystemConsole : subsystemGui; 69 70 try (InputStream inputStream = new FileInputStream(path.toString())) { 71 byte[] bytes = new byte[bufferSize]; 72 Test.assertEquals(bufferSize, inputStream.read(bytes), 73 String.format("Check %d bytes were read from %s file", 74 bufferSize, path)); 75 76 // Check PE header for console or Win GUI app. 77 // https://docs.microsoft.com/en-us/windows/desktop/api/winnt/ns-winnt-_image_nt_headers 78 for (int i = 0; i < (bytes.length - 4); i++) { 79 if (bytes[i] == 0x50 && bytes[i + 1] == 0x45 |