日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

如何使用 Java 中执行 Windows 的 CMD 命令

發布時間:2024/4/13 windows 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何使用 Java 中执行 Windows 的 CMD 命令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何使用 Java 中執行 Windows 的 CMD 命令

  • 核心代碼
  • 完整代碼

??在 CMD 中執行 BAT 腳本對用戶不友好,而且有安全隱患,因此筆者編寫了一些可以在 Java 中執行 Windows 的 CMD 命令的 API。

核心代碼

  • 執行單條命令
package org.wangpai.demo;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.stream.Collectors; import lombok.AccessLevel; import lombok.Getter; import lombok.experimental.Accessors;/*** @since 2021-12-21*/ @Accessors(chain = true) public class WinCmd {private Charset cmdCharset;@Getter(AccessLevel.PROTECTED)private Process process;private String originalCommandMsg;private String output;private int exitValue;private WinCmd() {super();}public static WinCmd getInstance() {return new WinCmd();}/*** 如果調用本方法時,本方法會阻塞調用線程,直到命令執行結束** @since 2021-12-21*/public int getExitValue() throws Exception {if (this.process == null) {throw new Exception("需要先執行命令后才能獲取退出碼");}this.exitValue = process.waitFor();return this.exitValue;}public String getOutput() throws Exception {if (this.process == null) {throw new Exception("需要先執行命令后才能獲取輸出");}if (this.output != null) {return this.output;}var inputStream = process.getInputStream();// 獲取操作系統(命令行)的字符集this.cmdCharset = Charset.forName((String) System.getProperties().get("sun.jnu.encoding"));// 將輸出按行分成多個字符串。這原本不是想要的操作,但這沒有辦法var lines = new BufferedReader(new InputStreamReader(inputStream, this.cmdCharset)).lines();this.output = lines.collect(Collectors.joining(System.lineSeparator()));// 將每行字符串以換行符拼接,還原出原始信息return this.output;}public String getCommand() throws Exception {if (this.originalCommandMsg == null) {throw new Exception("還沒有輸入命令");}return this.originalCommandMsg;}public WinCmd execute(String command) throws Exception {return this.exec(command);}private WinCmd exec(String originalCommand) throws IOException {this.output = null;this.originalCommandMsg = originalCommand;this.process = Runtime.getRuntime().exec(this.originalCommandMsg);return this;} }
  • 執行多條命令
package org.wangpai.demo;import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.stream.Collectors; import java.util.stream.Stream;/*** @since 2021-12-21*/ public class WinBat {private int commandNum;private Charset cmdCharset;private String[] originalCommandMsg;private Process[] processArray;private String[] outputs;private int[] exitValues;private WinBat() {super();}public static WinBat getInstance() {return new WinBat();}public String getIntegrationOutput() throws Exception {if (this.processArray == null) {throw new Exception("需要先執行命令后才能獲取輸出");}if (this.outputs == null) {// 獲取操作系統(命令行)的字符集this.cmdCharset = Charset.forName((String) System.getProperties().get("sun.jnu.encoding"));this.outputs = new String[this.commandNum];InputStream inputStream;for (int index = 0; index < this.commandNum; ++index) {inputStream = this.processArray[index].getInputStream();// 將輸出按行分成多個字符串。這原本不是想要的操作,但這沒有辦法var linesOutput = new BufferedReader(new InputStreamReader(inputStream, this.cmdCharset)).lines();// 將每行字符串以換行符拼接,還原出原始信息this.outputs[index] = linesOutput.collect(Collectors.joining(System.lineSeparator()));}}var stream = Stream.of(this.outputs);// 多加一個空行作不同命令輸出之間的分隔return stream.collect(Collectors.joining(System.lineSeparator() + System.lineSeparator()));}public String getIntegrationCommand() throws Exception {if (this.originalCommandMsg == null) {throw new Exception("還沒有輸入命令");}var stream = Stream.of(this.originalCommandMsg);return stream.collect(Collectors.joining(System.lineSeparator()));}public String[] getOriginalCommand() throws Exception {if (this.originalCommandMsg == null) {throw new Exception("還沒有輸入命令");}return this.originalCommandMsg;}/*** 如果調用本方法時,本方法會阻塞調用線程,直到命令執行結束** @since 2021-12-21*/public int[] getExitValues() throws Exception {if (this.processArray == null) {throw new Exception("需要先執行命令后才能獲取退出碼");}this.exitValues = new int[this.commandNum];for (int index = 0; index < this.commandNum; ++index) {this.exitValues[index] = this.processArray[index].waitFor();}return this.exitValues;}/*** 如果調用本方法時,本方法會阻塞調用線程,直到命令執行結束** @since 2021-12-21*/public int getLastExitValues() throws Exception {if (this.processArray == null) {throw new Exception("需要先執行命令后才能獲取退出碼");}var exitValues = this.getExitValues();return exitValues[this.commandNum - 1];}private WinBat exec(String[] originalCommand) throws Exception {this.outputs = null;this.originalCommandMsg = originalCommand;this.commandNum = originalCommand.length;this.processArray = new Process[this.commandNum];for (int index = 0; index < this.commandNum; ++index) {this.processArray[index] = WinCmd.getInstance().execute(this.originalCommandMsg[index]).getProcess();}return this;}public WinBat execute(String[] command) throws Exception {return this.exec(command);} }

完整代碼

??已上傳至 GitCode 中,可免費下載:https://gitcode.net/wangpaiblog/20220110-win_cmd

總結

以上是生活随笔為你收集整理的如何使用 Java 中执行 Windows 的 CMD 命令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。