3

ProcessBuilder 只是挂起并且没有完成。我看过很多关于这个的文章,但我仍然没有成功解决这个问题。任何人都可以看到这个问题或有建议吗?

我正在尝试执行一个批处理文件,该批处理文件为活动目录上的用户启用邮件。

代码:

    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JButton button = new JButton("TEST");
    button.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent arg0) {

            System.out.println("Starting process");

            ProcessBuilder pb = new ProcessBuilder(
                    "cmd.exe",
                    "/C",
                    "Y:\\mail-enable-users-groups.bat");
            Process process = null;
            try 
            {
                process = pb.start();

                ProcessOutputThread t = new ProcessOutputThread(process.getInputStream(), new StringBuffer());
                t.start();

                process.waitFor();
                t.interrupt();
            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            System.out.println("Process ended");
        }
    });

    frame.add(button);
}

private static class ProcessOutputThread extends Thread
{
    private StringBuffer m_output;
    private InputStream m_inputStream;

    public ProcessOutputThread(InputStream inputstream, StringBuffer output) 
    {
        super( "ProcessOutputThread" );
        m_inputStream = inputstream;
        m_output = output;
    }

    @Override
    public void run() {
        byte[] buffer = new byte[ 8192 ];

        try {
            while( true )
            {
                int available = m_inputStream.available();

                if( available == 0 )
                {
                    try
                    {
                        Thread.sleep( 100 );
                    }
                    catch( InterruptedException e )
                    {
                        break;
                    }

                    continue;
                }

                int len = Math.min( buffer.length, available );
                len = m_inputStream.read( buffer, 0, len );
                String outString = new String( buffer, 0, len );
                m_output.append( outString );

                System.out.println(outString);
            }
        }
        catch( IOException e ) 
        {
            e.printStackTrace();
        }
    }
}

批处理文件:

PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-User -RecipientTypeDetails User -Filter { UserPrincipalName -ne $Null } | Enable-Mailbox

输出:

Y:\>PowerShell.exe -command ". 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1'; Connect-ExchangeServer -auto; Get-User -RecipientTypeDetails User -Filter { UserPrincipalName -ne $Null } | Enable-Mailbox
4

1 回答 1

3

您正在等待您的进程,但该进程会生成一些输出并很快阻塞,直到您读取该输出。这是一个死锁,您必须通过在调用之前读取输出来解决p.waitFor()

处理输出有点棘手,因为您必须等待它。在您的循环的第一次迭代中,可能没有输出,因为您的外部程序需要一些时间来生成它。所以readline()将返回null,您的程序将退出。

过去,我通过产生一个读取输出的线程来处理这些情况。在主线程中,我做了一个p.waitFor()像你一样的事情。

我建议使用apache exec而不是自己处理。


更新:如果你必须自己做,方法是:

启动一个单独的线程并读取输出。不要停止阅读。在您的主线程调用p.waitFor()中,然后join()在您的线程上调用。捕获InterruptedException线程内部时,请确保您阅读了所有可用的输出直到最后,然后终止您的线程。

示例代码:

private static class C_ProcessOutputThread extends Thread {
    private StringBuffer m_output;
    private InputStream m_inputStream;

    public C_ProcessOutputThread(
            InputStream inputstream,
            StringBuffer output
    ) {
        super( "ProcessOutputThread" );
        m_inputStream = inputstream;
        m_output = output;
    }

    @Override
    public void run() {
        byte[] buffer = new byte[ 8192 ];

        try {
            while( true ) {
                int available = m_inputStream.available();

                if( available == 0 ) {
                    try {
                        Thread.sleep( 100 );
                    }
                    catch( InterruptedException e ) {
                        break;
                    }

                    continue;
                }

                int len = Math.min( buffer.length, available );
                len = m_inputStream.read( buffer, 0, len );
                String outString = new String( buffer, 0, len );
                m_output.append( outString );
            }
        }
        catch( IOException e ) {
            e.printStackTrace();
        }
    }
}
于 2013-08-24T21:55:17.097 回答