0

So for example:

Runtime rt = Runtime.getRuntime(); creates Runtime rt

Process p1 = rt.exec("C:/Windows/System32/calc.exe"); creates Process p1 on Runtime rt.

Then p1.destroy(); will destroy Process p1.

My question is: If I have more than one Process (e.g. p1, p2, and p3), how do I destroy them all at once, instead of having to destroy them one by one?

knorberg
  • 462
  • 5
  • 19

1 Answers1

2

Keep a List<Process> of all your processes and destroy them in a loop.

List<Process> processes = ...

for(Process p : processes) {
    p.destroy();
}
Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • So if I have all of my processes in a method called `Processes()`, could I create another method `destroyProcesses()` and put the loop there? Also, how would I reference the Processes from `destroyProcesses()`? – knorberg Jul 03 '13 at 20:10
  • @user2506658 You would need to keep a reference to a [`List`](http://docs.oracle.com/javase/7/docs/api/java/util/List.html) of all your processes. Then you could access that list from a method, such as `destroyProcesses`, and do the work there. You might be interested in some basic [Java tutorials](http://docs.oracle.com/javase/tutorial/) to help you. – Jeffrey Jul 03 '13 at 20:16
  • Also, I have never worked with lists before, and I can't find how to do it. Can you show me basically how to use `List processes = ...` for `p1`, `p2`, and `p3`? – knorberg Jul 03 '13 at 20:22
  • @user2506658 Here's the [Collections](http://docs.oracle.com/javase/tutorial/collections/) tutorial. It will show you how to use a `List`. – Jeffrey Jul 03 '13 at 20:26
  • Another thing, I haven't really worked with arrays before either... Could you give me a basic example? – knorberg Jul 03 '13 at 20:30
  • @user2506658 And here's the [array](http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html) tutorial. Try [searching](http://docs.oracle.com/javase/tutorial/search.html) through the tutorials the next time you have a problem. – Jeffrey Jul 03 '13 at 20:31
  • Ok, I am getting very lost and confused... If I give you the PasteBin link for my code, could you explain a little more on how to do this? – knorberg Jul 03 '13 at 20:55