|
| 1 | +package com.jgcomptech.tools; |
| 2 | + |
| 3 | +import com.jgcomptech.tools.osinfo.CheckIf; |
| 4 | +import com.sun.jna.platform.win32.WinDef; |
| 5 | +import org.jetbrains.annotations.Nullable; |
| 6 | + |
| 7 | +import java.io.*; |
| 8 | +import java.util.*; |
| 9 | + |
| 10 | +public class CommandInfo { |
| 11 | + /** Output object that is returned after the command has completed */ |
| 12 | + public static class Output { |
| 13 | + /** Returns the text result of the command. */ |
| 14 | + public final ArrayList<String> Result = new ArrayList<>(); |
| 15 | + |
| 16 | + /** Returns the exit code. Returns 0 if no error occurred. */ |
| 17 | + public int ExitCode = 0; |
| 18 | + |
| 19 | + public void print() { |
| 20 | + for(String line : this.Result) { |
| 21 | + System.out.println(line); |
| 22 | + } |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Runs command and returns results to ArrayList in Output object. |
| 28 | + * @param command Command to run. |
| 29 | + * @param args Arguments to pass to command. |
| 30 | + * @return Output object |
| 31 | + */ |
| 32 | + public static Output Run(String command, String args) |
| 33 | + { |
| 34 | + return Run(command, args, false, true, false); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Runs command elevated, shows cmd window and pauses window when command is complete. |
| 39 | + * If "elevate" parameter is false, it is ignored and and results will be saved to Output object. |
| 40 | + * If OS is not Windows, "elevate" parameter is ignored and results will be saved to Output object. |
| 41 | + * @param command Command to run. |
| 42 | + * @param args Arguments to pass to command. |
| 43 | + * @param elevate Boolean to set if command should be run elevated. If true Output object will be empty. |
| 44 | + * @return Output object |
| 45 | + */ |
| 46 | + public static Output Run(String command, String args, boolean elevate) |
| 47 | + { |
| 48 | + if(elevate) { |
| 49 | + return Run(command, args, true, false, true); |
| 50 | + } |
| 51 | + return Run(command, args); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Runs command according to parameters. Will only open cmd window if OS is Windows. |
| 56 | + * If OS is not Windows, all boolean parameters are ignored and results will be saved to Output object. |
| 57 | + * @param command Command to run. |
| 58 | + * @param args Arguments to pass to command. |
| 59 | + * @param elevate Boolean to set if command should be run elevated. |
| 60 | + * If true Output object will be empty. |
| 61 | + * @param hideWindow If true, cmd window will be hidden. |
| 62 | + * If true, and elevate is false, results will be saved to Output object. |
| 63 | + * @param keepWindowOpen If true, pauses cmd window and forces it to stay open after command is completed. |
| 64 | + * If false and "elevate" is true, cmd window will close after command is completed. |
| 65 | + * |
| 66 | + * This parameter is ignored if "hidewindow" is true. |
| 67 | + * This prevents cmd window from staying open when hidden and unnecessarily using RAM. |
| 68 | + * @return Output object |
| 69 | + */ |
| 70 | + @Nullable |
| 71 | + public static Output Run(String command, String args, boolean elevate, boolean hideWindow, boolean keepWindowOpen) { |
| 72 | + Output newOutput = new Output(); |
| 73 | + |
| 74 | + if((elevate || !hideWindow) && CheckIf.isWindows()) { |
| 75 | + ShellExecute(command, args, elevate, hideWindow, keepWindowOpen ); |
| 76 | + } |
| 77 | + else { |
| 78 | + final Process process; |
| 79 | + try { |
| 80 | + if(CheckIf.isWindows()) { |
| 81 | + process = Runtime.getRuntime().exec("cmd /C \"" + command + " " + args + "\""); |
| 82 | + } else { |
| 83 | + process = Runtime.getRuntime().exec(command); |
| 84 | + } |
| 85 | + |
| 86 | + assert process != null; |
| 87 | + final BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 88 | + |
| 89 | + String line; |
| 90 | + while((line = br.readLine()) != null) { |
| 91 | + newOutput.Result.add(line); |
| 92 | + } |
| 93 | + |
| 94 | + process.waitFor(); |
| 95 | + |
| 96 | + newOutput.ExitCode = process.exitValue(); |
| 97 | + } catch(IOException | InterruptedException e) { |
| 98 | + e.printStackTrace(); |
| 99 | + } |
| 100 | + } |
| 101 | + return newOutput; |
| 102 | + } |
| 103 | + |
| 104 | + private static void ShellExecute(String command, String args, boolean elevate, boolean hideWindow, boolean keepWindowOpen) { |
| 105 | + FileWriter writer; |
| 106 | + try { |
| 107 | + writer = new FileWriter("my.bat"); |
| 108 | + writer.write("@Echo off" + System.getProperty("line.separator")); |
| 109 | + writer.write("\"" + command + "\" " + args + System.getProperty("line.separator")); |
| 110 | + if(keepWindowOpen && !hideWindow) { writer.write("pause"); } |
| 111 | + writer.close(); |
| 112 | + } catch(IOException e) { |
| 113 | + e.printStackTrace(); |
| 114 | + } |
| 115 | + |
| 116 | + int WindowStatus = hideWindow ? 0 : 1; |
| 117 | + String operation = elevate ? "runas" : "open"; |
| 118 | + |
| 119 | + WinDef.HWND hw = null; |
| 120 | + String filename = "my.bat"; |
| 121 | + NativeMethods.Shell32.INSTANCE.ShellExecute(hw, operation, filename, null, null, WindowStatus); |
| 122 | + try { |
| 123 | + Thread.sleep(2000); |
| 124 | + } catch(InterruptedException e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + File file = new File("my.bat"); |
| 128 | + file.delete(); |
| 129 | + } |
| 130 | +} |
0 commit comments