Use false for waitForEnd to have an asynchroneous launch:
public static int runProcess(String commandLine, boolean waitForEnd,
StringBuilder output)
{
int rc = 0;
boolean b = true;
boolean r = true;
Process prc = null;
BufferedReader comeIn = null;
BufferedWriter goOut = null;
Date started = new Date();
try
{
prc = Runtime.getRuntime().exec(commandLine);
}
catch (IOException ex)
{
//Log error
}
if (waitForEnd)
comeIn = new BufferedReader(new InputStreamReader
(prc.getInputStream())); //We expect the OS default encoding here
although recommended to specify format always.
if (output != null && comeIn != null)
{
String s = "";
String ls = System.getProperty("line.separator", "\n");
do
{
try
{
s = comeIn.readLine();
if (s != null)
{
output.append(s);
output.append(ls);
}
}
catch (IOException ex)
{
//Log error
b = false;
break;
}
} while (s != null);
try
{
if (comeIn != null) comeIn.close();
}
catch (IOException ex)
{
//Log error
}
}
if (waitForEnd)
{
while (r)
{
try
{
rc = prc.exitValue();
r = false;
}
catch (IllegalThreadStateException ex)
{
//Process not finished yet
//Log error
}
}
}
if (!b)
{
//Log error
}
return rc;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---