Running p4 from Commad Prompt
I am calling the java main class through a batch script and my program calls the perforce (p4) sync operation. But when I close the command prompt in the middle of the sync operation, it terminates the java program but not the perforce sync process....Is there a way to terminate both?
I am calling sync operation through Runtime.exec()...
thanks
When you call Runtime.exec(), keep a reference to the Process object that it returns. Then, when your main class is shut down, have a shutdown hook call the destroy() method of that Process.You add a shutdown hook by calling Runtime's addShutdownHook method.
I wrote it like this....and doesn't seem to do the thing. I am pretty sure I am doing something totally wrong here...Can you see the problem?
=========================================
public static void main(String[] args) {
File f;
Process p = null;
try
{
String line;
Runtime run = Runtime.getRuntime();
p = run.exec("p4.exe -c ClientSpec sync -f");
BufferedReader input =
new BufferedReader
(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {
System.out.println(line+"\n");
}
p.waitFor();
try {
if (p.waitFor() != 0) {
System.err.println("exit value = " +
p.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
run.addShutdownHook(new Thread());
}
catch(Exception e)
{
System.err.print(e);
}
p.destroy();
========================================
thanks
nvm I got it to working....
For those who require the method
here it is:
final Process p = run.exec("p4.exe -c clientspec sync -f");
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
p.destroy();
}
}));
Thanks for the help DrClap :)
Message was edited by:
Duwahrahan