How To: Code a Simple Java App to Kill Any Process After a Specified Time

Code a Simple Java App to Kill Any Process After a Specified Time

How to Code a Simple Java App to Kill Any Process After a Specified Time

I may be a bit weird, but I enjoy listening to music at night as I fall asleep. Sure, you could create a playlist of songs so that it stopped after all the songs finished playing, but I have a rather extensive database of music and I enjoy listening to them randomly. Also, I like to ensure it terminates after a specific amount of time (I don't want it playing all night). Or sometimes I use it when I am cooking so that when the music stops, I know I need to check on my food, etc.

I primarily use this to kill my music player after a certain amount of time, but it can easily be changed to kill any process that you choose (see below).

Feel free to download the following for your enjoyment: source-code, Spotify, and VLC. The source-code is a .java file of the code used to create the program. Spotify and VLC kill the given programs after a specified amount of time. They are .jar files, so you will need to have Java installed on your machine, which most people will have already. You can view the source-code using a text editor, but will need an IDE if you want to create your own program. I recommend Eclipse.

Warning

  • Be careful of what processes you terminate, we are not responsible for any damage you may inadvertently cause.

How the Code Works

There is really only one thing that you really need to be worried about, and that is what process you are killing. Refer to the execKill() method:

On line #26, the string "TASKKILL /F /IM spotify.exe" tells the program what command to execute after a certain amount of time. All you really need to do is change "spotify.exe" to whatever process you want to kill. You can view all processes currently running by pressing ctrl+alt+del.

Example with Firefox

Suppose you wanted to close your web-browser after 15 minutes, here are the detailed steps on how to do this:

Step 1: Getting Java Ready

  1. Make sure you have downloaded Java and an IDE (Eclipse/NetBeans), then open up your IDE.
  2. In the package explorer, right-click and select "New" -> "Java Project". Name it whatever you want to, then click "Finish".
  3. Right-click your new project and select "New" -> "Package". Name it something useful, perhaps the name of the program you want to kill.
  4. Right-click the package you just selected and select "New" -> "Class". You can name this whatever you want, but if you name it something other than "spotifyKiller", make sure to change it in the source code.

Step 2: Readying the Code

  1. Download the source-code file (above), if you haven't already.
  2. Open it with a text editor, such as WordPad or Notepad.
  3. Press ctrl+a (selects all), then ctrl+c (copies code).
  4. Go to the IDE and inside the class and put your cursor between the two curly braces by clicking there and then press ctrl+v to paste what we copied.

Step 3: Modifying the Code

  1. As in the "How the Code Works", change spotify.exe to firefox.exe.
  2. Press ctrl+s to save the code, then open an instance of Firefox.
  3. Click the "Run" button in Eclipse (it looks like a green play button). This should cause a window to pop up. Type "1" into the box and press "Start" and wait 1 minute. If all went well Firefox will have closed. If so, proceed, otherwise go back over the steps and try to figure out what went wrong.

Step 4: Exporting

Now your code should be working as we want. We don't want to have to run the code from our IDE every time, so we are going to export it to a runnable file:

  1. Make sure your code is saved.
  2. Look in the top left-hand corner of Eclipse and select "File" -> "Export...".
  3. A window will pop up, when this happens select "Runnable Jar File" and then "Next".
  4. Make sure your screen looks something like the picture below. You can specify where you want to be exported, so you can find it easily later on:
  1. Finally, click "Finish"!

Step 5: Running the Program

Now all you have to do is double click on the file you just created and input how many minutes you want until Firefox should close. For this example, we put "15" and click "Start" and voilà! Firefox closes after 15 minutes have passed, yay!

Photo by twentyeighttwelve

9 Comments

Clean & simple. To play a (somewhat) evil trick on your roommate, you could write one of these to kill the "explorer" process after 5 minutes, and add a shortcut to it in your roommate's windows startup directory. 5 Minutes after logging into their computer, your unsuspecting victim's start menu and task bar will vanish!

For extra laughs, have a stop watch handy & start it when they login, Just before you hit the 5 minute mark, snap your fingers and watch their horror as you "magically" kill their start menu :)

Haha, yes you definitely could do that!? :) You would probably need to hardcode the time into it and remove the popup functionality so it doesn't pop up when the computer boots, but that wouldn't be too hard. I am glad my programming is being put to "good use"? haha

That is a good use? Ok then.
I DID NOT say i didnt like the ideia and my dad is quite bad with computers i think he would not like it...

Are the files still available on a different file host? The links are dead for mediafire

The links should work properly now! :)

Thank you so much, I enjoy your articles!

Thanks! I always mean to write up some new articles, I always have new ideas popping into my head, but school is just sooooo time consuming! ;)

i am unable to download the source code code

Share Your Thoughts

  • Hot
  • Latest