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

相关内容

热门资讯

透视指南书!hhpoker作必... 透视指南书!hhpoker作必弊码,hhpoker哪个俱乐部靠谱(透视)都是是有挂(哔哩哔哩)1、该...
透视项目!aapoker透视方... 透视项目!aapoker透视方法,aa poker透视软件(透视)一直有脚本方法(哔哩哔哩)aa p...
透视窍门!hhpoker怎么防... 透视窍门!hhpoker怎么防作必弊(透视)开挂透视插件(哔哩哔哩)1、玩家可以在hhpoker怎么...
玩家必知教程!"心动... 玩家必知教程!"心动休闲辅助"原来是有辅助工具(揭秘有挂)-哔哩哔哩1、心动休闲辅助透视辅助软件激活...
透视方案!wejoker辅助器... 透视方案!wejoker辅助器怎么卖,德扑之心免费透视(透视)确实真的有挂(哔哩哔哩)1、wejok...
透视要领!wepoker私人局... 透视要领!wepoker私人局有透视吗(透视)开挂透视软件(哔哩哔哩)1、下载好wepoker私人局...
透视阶段!wpk可以作必弊吗,... 透视阶段!wpk可以作必弊吗,wpk辅助插件(透视)一直存在有脚本挂(哔哩哔哩)1、全新机制【wpk...
据了解"518互游辅... 据了解"518互游辅助"切实真的有辅助挂(有挂技巧)-哔哩哔哩1、518互游辅助辅助器安装包、518...
透视手筋!wejoker辅助脚... 透视手筋!wejoker辅助脚本,pokemmo手机版脚本免费(透视)切实真的有挂(哔哩哔哩)1、p...
透视操作!aapoker透视方... 透视操作!aapoker透视方法,aapoker怎么拿好牌(透视)确实有透视插件(哔哩哔哩);aap...