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.
- Be careful of what processes you terminate, we are not responsible for any damage you may inadvertently cause.
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.
Suppose you wanted to close your web-browser after 15 minutes, here are the detailed steps on how to do this:
- Make sure you have downloaded Java and an IDE (Eclipse/NetBeans), then open up your IDE.
- In the package explorer, right-click and select "New" -> "Java Project". Name it whatever you want to, then click "Finish".
- Right-click your new project and select "New" -> "Package". Name it something useful, perhaps the name of the program you want to kill.
- 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.
- Download the source-code file (above), if you haven't already.
- Open it with a text editor, such as WordPad or Notepad.
- Press ctrl+a (selects all), then ctrl+c (copies code).
- 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.
- As in the "How the Code Works", change spotify.exe to firefox.exe.
- Press ctrl+s to save the code, then open an instance of Firefox.
- 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.
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:
- Make sure your code is saved.
- Look in the top left-hand corner of Eclipse and select "File" -> "Export...".
- A window will pop up, when this happens select "Runnable Jar File" and then "Next".
- 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:
- Finally, click "Finish"!
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!
- Thank you for reading! Stay tuned for more Invisible Computer articles!