从Java调用Powershell脚本 [英] Invoke Powershell scripts from Java

查看:640
本文介绍了从Java调用Powershell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从java调用我的powershell脚本。能做到吗?我尝试使用以下代码,但流未关闭。

I want to invoke my powershell script from java. Can it be done. I tried with the following code, but the stream is not closing.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class TestPowershell {

    public static void main(String[] args) throws IOException 
    {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("powershell C:\\testscript.ps1");
        InputStream is = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader reader = new BufferedReader(isr);
        String line;
        while ((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }
        reader.close();
        proc.getOutputStream().close();
    }

}

java是否会调用powershell脚本执行创建远程会话并执行cmdlet?

Does java invoke a powershell script which performs create remote session and execute cmdlets?

我们是否支持在java中调用powershell脚本?

Do we have support to invoke powershell scripts in java?

任何人你能帮忙吗?

等待你的回复。

谢谢,
rammj

Thanks, rammj

推荐答案

启动流程后( runtime.exec()),添加一行关闭进程的输入流(JAVA调用输出流!!):

After starting the process ( runtime.exec() ), add a line to close the input stream of the process ( which JAVA calls output stream!!):

 proc.getOutputStream().close();

这篇关于从Java调用Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆