gpt4 book ai didi

尝试调用 powershell 脚本时 Java 程序挂起

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:19:44 29 4
gpt4 key购买 nike

我正在使用 NetBeans 构建 GUI,而 GUI 中的其中一个按钮需要使用 powershell 脚本。我正在尝试获取脚本的输出并将其放入 GUI 中的 JTextArea 中。这是我到目前为止所拥有的。我做了一些调试,它似乎卡在 while 循环内,但我不明白为什么会这样。

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("cmd powershell C:/hello1.ps1");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line;
while ((line = reader.readLine()) != null) {
outputTextArea.setText(line);
}
reader.close();
proc.getOutputStream().close();
} catch (IOException ex) {
Logger.getLogger(BatchFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

这里是一个简单的 powershell 脚本,我正在尝试让它工作。

#Filename: hello1.ps1
Write-Host "Hello World!"
#End of Script

我做了一些研究,我注意到其他人挂起,但这只是因为他们忘记关闭进程输出流。

最佳答案

我遇到了同样的问题。我在 while 循环之前移动了 proc.getOutputStream().close() 并且一切正常

关于尝试调用 powershell 脚本时 Java 程序挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14139759/

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