gpt4 book ai didi

Java 命令提示符命令抛出 "No such File or Directory"

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:31:46 27 4
gpt4 key购买 nike

我目前正在开发一个应该执行一些控制台命令的程序。

我的代码是这样的:

  private String executeCommands(String[] commands)
{
String result = "";
try
{
ProcessBuilder pb = new ProcessBuilder();
String s = null;
Charset charset = Charset.forName("IBM850");
BufferedReader stdInput;
Process proc;
for (String command : commands)
{
System.out.println("Ausfuehrung von: " + command);
pb.command(command);
proc = pb.start();
stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream(), charset));
while ((s = stdInput.readLine()) != null)
{
result += s;
}
System.out.println();
}
}
catch (Exception ex)
{
result = ex.getMessage();
}
return result;
}
private void userLogIn(IUserInteraction userInteraction)
{
String[] command = { "svn auth --show-passwords" };
String result = executeCommands(command);
System.out.println(result);
}

输出是“无法运行程序“svn auth --show-passwords”:error=2,没有那个文件或目录”,但是当我在控制台手动输入命令时,它起作用了。我做错了什么?

提前致谢!

最佳答案

经过一些尝试,我找到了一个可行的解决方案

String result
String line;
Process process = Runtime.getRuntime().exec(command);
Reader r = new InputStreamReader(process.getInputStream());
BufferedReader in = new BufferedReader(r);
while ((line = in.readLine()) != null)
result += line;
in.close();

关于Java 命令提示符命令抛出 "No such File or Directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57144795/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com