Java 解决 Process 执行命令行命令报【CreateProcess error=2, 系统找不到指定的文件。】错误问题
创始人
2025-01-15 18:07:43
0

目录

问题

问题代码

解决方案

判断操作系统


问题

使用 Process 执行命令行命令时,报 CreateProcess error=2, 系统找不到指定的文件。但明明指定的文件是存在的。而且这种错误只在 IDEA 中运行会报错,打包后直接 java -jar 运行就能正常运行,不会报错。

问题代码

public class MyTest {      public static void main(String[] args) throws Exception {         Path dirPath = Paths.get("D:\\workspace\\funcproject");         ProcessBuilder pb = new ProcessBuilder();         pb.directory(dirPath.toFile());         pb.command("fn", "version");          Process process = pb.start();          try (BufferedReader reader1 = new BufferedReader(new InputStreamReader(process.getInputStream()));              BufferedReader reader2 = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {             String line;             while ((line = reader1.readLine()) != null) {                 System.out.println(line);             }              while ((line = reader2.readLine()) != null) {                 System.out.println(line);             }          } catch (IOException e) {             e.printStackTrace();         }          process.waitFor();      }  }  

解决方案

以上面的问题代码为例,执行的命令行命令 fn 不是有效的可执行文件,而是由命令行 shell ( cmd.exe) 提供的命令。为了从其他进程运行此命令,必须启动cmd.exe并将参数传递给它。

在 Windows 开发环境下,需要在命令参数前面增加两个参数:"cmd.exe", "/C"

pb.command("cmd.exe", "/C", "fn", "version"); 

在 Linux 开发环境下,则新增两个参数为:"bash", "-c"

pb.command("bash", "-c", "fn", "version"); 

判断操作系统

打包后其实能正常运行,代码其实无需做调整。调试时可以在 IDEA 中将代码先改成可以正常运行的(先传入cmd.exe),然后在提交代码时再重新改回来。不过这样显然很麻烦,直接在通过代码来判断,先获取获取当前操作系统,然后根据不同的操作系统传入不同的 shell 名字。

public class MyTest {      public void execute(String Command) throws Exception {         ProcessBuilder pb = new ProcessBuilder();          List cmd = new ArrayList<>(); 		String shell = getShellBySystem(); 		cmd.add(shell); 		cmd.add(shell.equals("bash") ? "-c" : "/C");         cmd.addAll(Arrays.asList(command.split(" ")));         pb.command(cmd);          Process process = pb.start();         process.getInputStream();         process.waitFor();     }      private String getShellBySystem() { 		String system = System.getProperty("os.name").toLowerCase(); 		if (system.contains("win")) { 			return "cmd.exe"; 		} else if (system.contains("linux")) { 			return "bash"; 		} else { 			throw new RuntimeException(String.format("未知系统 %s", system)); 		} 	}  }  

相关内容

热门资讯

透视法门!德州局透视脚本,hh... 透视法门!德州局透视脚本,hhpoker德州有挂吗(透视)切实是有挂(哔哩哔哩)hhpoker德州有...
第9分钟辅助!微信小程序黑科技... 第9分钟辅助!微信小程序黑科技,微信小程序微乐自建房脚本下载(原来存在有外挂安装)-哔哩哔哩1、用户...
透视办法!aapoker俱乐部... 透视办法!aapoker俱乐部靠谱吗,aapoker万能辅助器(透视)竟然有脚本方法(哔哩哔哩)1、...
透视指南书!wepoker线上... 透视指南书!wepoker线上大神,wepoker私人局透视方法(透视)切实真的有挂(哔哩哔哩)进入...
第七分钟安装!微乐自建房脚本免... 第七分钟安装!微乐自建房脚本免费下载,微乐智能插件激活码(切实是真的外挂平台)-哔哩哔哩小薇(辅助器...
透视指引!德普之星怎么设置埋牌... 透视指引!德普之星怎么设置埋牌,wepoker能不能透视(透视)一贯真的有挂(哔哩哔哩)1、游戏颠覆...
透视攻略!wepoker有透视... 透视攻略!wepoker有透视吗,htx矩阵wepoker辅助(透视)确实是有脚本挂(哔哩哔哩)1、...
8分钟安装!微乐小程序黑科技,... 8分钟安装!微乐小程序黑科技,微乐小程序黑科技app入口(都是是有外挂安装)-哔哩哔哩1、首先打开微...
透视手筋!购买wepoker模... 透视手筋!购买wepoker模拟器,wepoker有插件吗(透视)真是真的是有挂(哔哩哔哩)购买we...
透视法子!aapoker插件,... 透视法子!aapoker插件,aapoker如何设置胜率(透视)总是是真的脚本技巧(哔哩哔哩)在进入...