|
| 1 | +package com.baeldung.manifest; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.BufferedWriter; |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileWriter; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStreamReader; |
| 9 | + |
| 10 | +public class ExecuteJarFile { |
| 11 | + |
| 12 | + private static final String DELIMITER = " "; |
| 13 | + private static final String WORK_PATH = "src/main/java/com/baeldung/manifest"; |
| 14 | + private static final String MANIFEST_MF_PATH = WORK_PATH + "/MANIFEST.MF"; |
| 15 | + private static final String MAIN_MANIFEST_ATTRIBUTE = "Main-Class: com.baeldung.manifest.AppExample"; |
| 16 | + |
| 17 | + private static final String COMPILE_COMMAND = "javac -d . AppExample.java"; |
| 18 | + private static final String CREATE_JAR_WITHOUT_MF_ATT_COMMAND = "jar cvf example.jar com/baeldung/manifest/AppExample.class"; |
| 19 | + private static final String CREATE_JAR_WITH_MF_ATT_COMMAND = "jar cvmf MANIFEST.MF example.jar com/baeldung/manifest/AppExample.class"; |
| 20 | + private static final String EXECUTE_JAR_COMMAND = "java -jar example.jar"; |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + System.out.println(executeJarWithoutManifestAttribute()); |
| 24 | + System.out.println(executeJarWithManifestAttribute()); |
| 25 | + } |
| 26 | + |
| 27 | + public static String executeJarWithoutManifestAttribute() { |
| 28 | + return executeJar(CREATE_JAR_WITHOUT_MF_ATT_COMMAND); |
| 29 | + } |
| 30 | + |
| 31 | + public static String executeJarWithManifestAttribute() { |
| 32 | + createManifestFile(); |
| 33 | + return executeJar(CREATE_JAR_WITH_MF_ATT_COMMAND); |
| 34 | + } |
| 35 | + |
| 36 | + private static void createManifestFile() { |
| 37 | + BufferedWriter writer; |
| 38 | + try { |
| 39 | + writer = new BufferedWriter(new FileWriter(MANIFEST_MF_PATH)); |
| 40 | + writer.write(MAIN_MANIFEST_ATTRIBUTE); |
| 41 | + writer.newLine(); |
| 42 | + writer.close(); |
| 43 | + } catch (IOException e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private static String executeJar(String createJarCommand) { |
| 49 | + executeCommand(COMPILE_COMMAND); |
| 50 | + executeCommand(createJarCommand); |
| 51 | + return executeCommand(EXECUTE_JAR_COMMAND); |
| 52 | + } |
| 53 | + |
| 54 | + private static String executeCommand(String command) { |
| 55 | + String output = null; |
| 56 | + try { |
| 57 | + output = collectOutput(runProcess(command)); |
| 58 | + } catch (Exception ex) { |
| 59 | + System.out.println(ex); |
| 60 | + } |
| 61 | + return output; |
| 62 | + } |
| 63 | + |
| 64 | + private static String collectOutput(Process process) throws IOException { |
| 65 | + StringBuffer output = new StringBuffer(); |
| 66 | + BufferedReader outputReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 67 | + String line = ""; |
| 68 | + while ((line = outputReader.readLine()) != null) { |
| 69 | + output.append(line + "\n"); |
| 70 | + } |
| 71 | + return output.toString(); |
| 72 | + } |
| 73 | + |
| 74 | + private static Process runProcess(String command) throws IOException, InterruptedException { |
| 75 | + ProcessBuilder builder = new ProcessBuilder(command.split(DELIMITER)); |
| 76 | + builder.directory(new File(WORK_PATH).getAbsoluteFile()); |
| 77 | + builder.redirectErrorStream(true); |
| 78 | + Process process = builder.start(); |
| 79 | + process.waitFor(); |
| 80 | + return process; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments