Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

About Runtime.getruntime.exe()?  RSS feed

 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String moveCommand="cmd /k move /y D:\\app1.class D:\\test\\doUpdate";
p1 = Runtime.getRuntime().exec(moveCommand);
p1.destroy();
System.exit(0);

that means use DOS command to move a file to another place, after that i will exit the process by using "System.exit(0); ". but when i set a breakpoint to monitor the program, it's going well.But when i really run the program it can not get the right result. finally i found the "move" command has a dely for 2-3s(2kb file). So beforce the dos finished its task, the System.exit(0) get work. so the program is shutdown but the file has not been move to the right place.
I want to ask if there are any method to check the status. Right after the exec() really finish the task , the program shutdown. i also see some apis in Process object. i used the waitfor() method but also give me a exception called "current thread not owner";
who can help me or give me any advices? thanks you very much
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
destroy() kills the process. Don't call destroy() immediately after Runtime.getRuntime().exec().

Call p1.waitFor() to wait until the process has finished. See the API documentation of class java.lang.Process for more info.

*edit*: Oh, I see you already tried waitFor(). Can you show us your complete code and the exact error message?

Did you accidentally call wait() instead of waitFor()?
[ August 27, 2007: Message edited by: Jesper Young ]
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait() is the object's method not the process. so i get a "current thread not owner". if i use the process method:waitFor(),the whole process is hang up and can not stopped and wake until i stopped it manually. but i get another ways to solve it.
p1 = Runtime.getRuntime().exec(moveCommand);
while(a.execute()){

}
a.exeute():
public boolean execute(){
File file=new File("D:\\app1.class");
long modifedTime=file.lastModified();
if(modifedTime==0){

return false;
}
else{
return true;

}


}
if the file exist the modifedTime return not 0a, so i check whether the file exist if not that means the exec has finished task, so i can destory it and exit the process. but i think this is a good way because it is not a currency solve method if i get another function to use i must find a another way to check if the exec finsh the task.
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i think i get it by using waitFor(). i think i forgot to clear and rebuilder the project.thank all.....
 
lei feng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry i make a mistake. I just change the command like this
String moveCommand="cmd /k move /y D:\\app1.class D:\\test\\doUpdate&exit";
after finish the task exit the process, not controled by the external
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!