gpt4 book ai didi

c# - 在java中使用进程

转载 作者:行者123 更新时间:2023-12-02 06:15:55 25 4
gpt4 key购买 nike

我正在将一些 C# 代码转换为 java

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
// Get the output into a string
string result = proc.StandardOutput.ReadToEnd();
// Display the command output.

这是我尝试将其转换为java的代码。

我不知道如何在java中运行命令和使用进程。我用谷歌搜索了一下,发现了类似的东西:

Process process = Runtime.getRuntime().exec(command);
Integer result = process.exitValue();

在行

process.exitValue()

它给了我java.lang.IllegalThreadStateException:进程尚未退出

最佳答案

exec之后,您需要使用process.waitFor()等待命令完成。

关于c# - 在java中使用进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479304/

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