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

相关内容

热门资讯

WPK透视辅助!德州局脚本,w... WPK透视辅助!德州局脚本,wpk德州局透视(切实是有挂)1、超多福利:超高返利,海量正版游戏,wp...
微扑克透视辅助!pokemmo... 微扑克透视辅助!pokemmo辅助器手机版下载,hhpoker开挂教程(一贯是真的有挂)1、微扑克透...
WPK透视辅助!扑克之星辅助,... WPK透视辅助!扑克之星辅助,wepoker辅助是真的假的(真是真的有挂)wepoker辅助是真的假...
wpk透视辅助!hhpoker... wpk透视辅助!hhpoker是内部控制吗,wepoker破解工具(好像真的有挂)1、hhpoker...
微扑克透视辅助!hhpoker... 微扑克透视辅助!hhpoker德州透视,xpoker透视辅助(好像真的是有挂)1)hhpoker德州...
微扑克透视辅助!黑侠破解wep... 微扑克透视辅助!黑侠破解wepoker,wepoker辅助分析器(切实是有挂)1、首先打开wepok...
wpk透视辅助!aapoker... wpk透视辅助!aapoker怎么设置抽水,大菠萝手游辅助(总是真的有挂)运aapoker怎么设置抽...
wpk透视辅助!wepoker... wpk透视辅助!wepoker软件辅助程序,wepoker辅助器安装包定制(其实是有挂)小薇(辅助器...
微扑克透视辅助!aapoker... 微扑克透视辅助!aapoker怎么提高中牌率,hhpoker软件安装包(确实真的有挂)1、进入游戏-...
WPK透视辅助!wepoker... 您好,wepoker透视器免费这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054...