java 调用 powershell 的问题 在线等 急

希望对你有帮助 希望对你有帮助 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

1、首先我本地有一个test.ps1的powershell文件,内容为(只是为了输出传进来的参数):

For($i=0;$i -lt $args.Count; $i++)
{
Write-Host "parameter $i : $($args[$i])"
}

2、然后我的java代码如下:
private void exeCmd() {
InputStream in = null;
BufferedReader reader = null;
try {
String cmd = "cmd /c powershell D:/test.ps1 p1 p2";
Process p = Runtime.getRuntime().exec(cmd);
in = p.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
System.out.println("out put end ---");
p.waitFor();
p.destroy();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

执行结果:
parameter 0 : p1
parameter 1 : p2

没有输出out put end ---,也就是说进程卡在 while里面了。后面所有内容都中断了。求大神帮忙。在下感激不尽。
...全文

相关推荐

评论 4
收藏 已收藏
分享
返回顶部
希望对你有帮助
Java EE 67,535 成员 225,843 内容