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

时间:2013-01-03 13:20:35

标签: java powershell netbeans

我正在使用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

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

4 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {      
    String allOutput = "";                                
    try {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("cmd /c powershell C:/hello1.ps1");
        BufferedReader errorReader = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
        BufferedReader outReader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line;
        while ((line = errorReader.readLine()) != null) {
            allOutput += "\n" + line;
        }
        while ((line = outReader.readLine()) != null) {
            allOutput += "\n" + line;
        }
        int retVal = proc.waitFor();
    } catch (IOException ex) {
        Logger.getLogger(BatchFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
    outputTextArea.setText(allOutput);
}
  • 使用CMD.EXE / c
  • 正确形成命令行
  • 检查ErrorStream
  • 使用Process.waitFor()读取Process类的java-docs。
  • 不需要关闭OutputStream,因为你从不使用它,程序不应该期望用户输入(java切换输入和输出的名称很烦人)

注意上面的代码未经过测试,因此可能存在语法错误等。

答案 2 :(得分:0)

这是我测试过的代码,注意选择" hack"完成后关闭STDIN。

package test;

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

public class Test
{
    private static boolean hack=false;

    public static void main(String[] args) throws IOException
    {
        Runtime rt = Runtime.getRuntime();

        String cmd[];

        if (hack)
            cmd=new String[]{"cmd","/c","C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe","-File","c:\\cygwin\\home\\jpyeron\\test.ps1", "<NUL"};
        else
            cmd=new String[]{"C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe","-File","c:\\cygwin\\home\\jpyeron\\test.ps1"};

        final Process p = rt.exec(cmd);

        Thread stdout = new Thread()
        {
            public void run() 
            {
                InputStream out = p.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(out)); 
                String line = null; 
                try
                {
                    while ((line = in.readLine()) != null) 
                    { 
                        System.out.println(line); 
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }              
            };
        };
        stdout.start();

        Thread stderr = new Thread()
        {
            public void run() 
            {
                InputStream err = p.getInputStream();
                BufferedReader in = new BufferedReader(new InputStreamReader(err)); 
                String line = null; 
                try
                {
                    while ((line = in.readLine()) != null) 
                    { 
                        System.out.println(line); 
                    }
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }                
            };
        };
        stderr.start();

        if (hack)
            ;
        else
            p.getOutputStream().close();
    }
}

答案 3 :(得分:0)

这对我有帮助:如果没有错误,请不要读取InputStream。 e.g。

private void takeAction () throws IOException, InterruptedException
{
    String action = getAction (); // A powershell-Command
    Process p = Runtime.getRuntime ().exec ( action );

    InputStream is = p.getErrorStream ();
    if ( 0 < is.available () )
    {
        BufferedReader br = new BufferedReader (
                new InputStreamReader ( is ) );
        String err = br.readLine ();
        while ( null != err )
        {
            System.out.println ( "takeAction() " + err );
            err = br.readLine ();
        }
        p.getOutputStream ().close ();
    }
}