ProcessBuilder只是挂起而无法完成。我已经看过很多关于此的文章,但我仍然没有成功解决这个问题。任何人都可以看到这个问题或有建议吗?
我正在尝试执行一个批处理文件,该文件可以在活动目录上启用邮件。
CODE:
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
答案 0 :(得分: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();
}
}
}