25 Jan 2018

Close Java AutoIt Script

If you are running a program that relies upon Java Runtime, sometimes it crashes and even using End Task doesn't seem to remove everything from memory. I was often having this problem so I wrote a small script with AutoIt to look for Java and close it, over and over again until it is gone from memory! You could adapt this script to close any troublesome program of course, just change the $proc= variable line in the script below.

Here's the script in case you find it useful:

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.10.2
 Author:         Michael Gerrard, http://mgxp.blogspot.com

 Script Function:
 Close a process

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here

$title  = "Close Java"
$proc  = "jp2launcher.exe"

If ProcessExists($proc) > 0 Then ;if it is running...

 ; Confirm that it should be closed
 $ok = MsgBox(33, $title, "Close " & $proc & "?")
 If $ok = 1 Then ;if OK
  While ProcessExists($proc) > 0
   ProcessClose($proc)
   Sleep(500) ;pause half a second
  Wend
 Else ;if Cancel
  Exit
 EndIf

 ; Check again to see if it is running now...
 If ProcessExists($proc) > 0 Then  ;it is still running
  MsgBox(0, $title, "Failed!")
 Else ;it is not running
  MsgBox(0, $title, "Success!")
 EndIf

Else ;if it is not running...
 MsgBox(0, $title, $proc & " is not running")
EndIf
Exit

Copy the above code, save it in a text file called CloseJava.au3

Compile using AutoIt https://autoitscript.com

Double click the resulting CloseJava.exe, it'll say:


Click OK





Conclusion
Just a small script that demonstrates how useful AutoIt is. Of course I've shown how to close Java but you could use the same script to close a different program. Just alter the $proc= variable.



No comments: