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)); 		} 	}  }  

相关内容

热门资讯

一分钟带你了解!wepoker... 一分钟带你了解!wepokerplus辅助,哈灵永久辅助(透视)必备开挂辅助神器;一分钟带你了解!w...
推荐一款!wepoker有没有... 推荐一款!wepoker有没有机器人,wepoker透视脚本苹果版(透视)起初有开挂辅助器(真是有挂...
透视规律!pokemmo手机版... 透视规律!pokemmo手机版透视脚本,hhpoker到底可以透视挂吗,解说技巧(本来有开挂辅助ap...
我来教大家!wepoker底牌... 我来教大家!wepoker底牌透视,能否雀神挂件可以安装(透视)教你开挂辅助神器;能否雀神挂件可以安...
实测发现!wepoker游戏的... 实测发现!wepoker游戏的安装教程,wepoker究竟有没有透视(透视)从前有开挂辅助挂(有开挂...
透视线上!德扑之星有辅助软件吗... 透视线上!德扑之星有辅助软件吗,wepoker透视脚本安卓,可靠技巧(其实有开挂辅助插件)1、下载好...
一分钟了解!pokemmo手机... 一分钟了解!pokemmo手机版脚本免费,九天大厅修改器(透视)解迷开挂辅助插件;pokemmo手机...
重大发现!wepoker数据分... 重大发现!wepoker数据分析,哈糖大菠萝有没有挂(透视)最初有开挂辅助下载(确实有挂);重大发现...
透视脚本!德州局hhpoker... 透视脚本!德州局hhpoker,wepoker可以透视码,新版2025教程(好像有开挂辅助插件)1、...
实操分享!智星德州辅助译码插件... 实操分享!智星德州辅助译码插件靠谱吗,佛手在线大菠萝技巧(透视)解谜开挂辅助挂;AI辅助机器人普及解...