发布时间:2025-06-24 16:54:20 作者:北方职教升学中心 阅读量:159
已处理,兼容windows和linux系统c;不会乱码。
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.List;public class CommandExecutorUtils { public static String executeCommand(String command) { Charset charset = System.getProperty("os.name").toLowerCase().contains("win") ? Charset.forName("GBK") : Charset.defaultCharset(); String os = System.getProperty("os.name").toLowerCase(); List<String> commands = new ArrayList<>(); // 判断操作系统类型并准备命令 if (os.contains("win")) { // Windows commands.add("cmd"); commands.add("/c"); } else { // Unix/Linux/MacOS commands.add("/bin/sh"); commands.add("-c"); } commands.add(command); // 执行命令并获得结果 try { ProcessBuilder processBuilder = new ProcessBuilder(commands); Process process = processBuilder.start(); // 获取输出 String output = readStream(process.getInputStream(), charset); // 等待过程结束 int exitCode = process.waitFor(); if (exitCode == 0) { return output; } else { String error = readStream(process.getErrorStream(), charset); throw new IOException("Command execution failed with exit code " + exitCode + " and error: " + error); } } catch (IOException | InterruptedException e) { Thread.currentThread().interrupt(); return "Error: " + e.getMessage(); } } private static String readStream(InputStream inputStream, Charset charset) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, charset)); StringBuilder builder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { builder.append(line); builder.append(System.getProperty("line.separator")); } return builder.toString(); } public static void main(String[] args) { // 示例命令 String command = "ping www.a.shifen.com"; // GBK编码读取Windows上的输出c;在Linux上使用默认编码系统 String result = CommandExecutorUtils.executeCommand(command); System.out.println(result); }}。