编程语言
首页 > 编程语言> > java-执行批处理脚本时,Processbuilder挂起

java-执行批处理脚本时,Processbuilder挂起

作者:互联网

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

解决方法:

您正在等待过程,但是该过程会生成一些输出,并且很快就会阻塞,直到您读取该输出.这是一个死锁,您必须在调用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();
        }
    }
}

标签:processbuilder,active-directory,java,batch-file
来源: https://codeday.me/bug/20191122/2063084.html