Java and Powershell


  • Getting process id for specific Java application
  • Kill Java applications
  • Monitoring Java applications

Process id

For this purpose, JPS – Java Virtual Machine Process Status Tool (Documentation) – is quite helpful. It can be found in the bin folder of the JDK/JRE. The following example shows the output while running Android Studio.

PS C:\Program Files\Java\jdk1.7.0_xx\bin> jps -v
9196  -Xms256m -Xmx1280m -XX:MaxPermSize=350m -XX:ReservedCodeCacheSize=225m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Djna.nosys=true -Djna.boot
.library.path= -Djna.debug_load=true -Djna.debug_load.jna=true -XX:+HeapDumpOnOutOfMemoryError -Didea
.paths.selector=AndroidStudioPreview1.4 -Didea.platform.prefix=AndroidStudio -Djb.vmOptions=C:\dev2\android-studio-1.4-beta2\bin\studio64.exe.vmoptions -Xbootclass
path/a:C:\dev2\android-studio-1.4-beta2\lib\boot.jar -Didea.jre.check=false
7072 GradleDaemon -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -Xmx1024m -Dfile.encoding=windows-1252 -Duser.language=de -Duser.variant
4304 Jps -Dapplication.home=C:\Program Files\Java\jdk1.7.0_79 -Xms8m

With the option “-v”, you get all arguments passed to the JVM, which is helpful to select the correct application. To get the process id from the Android Studio, we first have to select the line for Android Studio with the grep-like command select-string and store it in a variable $javaActive. As the selector -Didea.platform.prefix=AndroidStudio is used:

PS C:\Program Files\Java\jdk1.7.0_xx\bin> $javaActive = jps -v | select-string "-Didea.platform.prefix=AndroidStudio"

9196  -Xms256m -Xmx1280m -XX:MaxPermSize=350m -XX:ReservedCodeCacheSize=225m -XX:+UseConcMarkSweepGC -XX:SoftRefLRUPolicyMSPerMB=50 -ea -Djna.nosys=true 
-Djna.boot.library.path= -Djna.debug_load=true -Djna.debug_load.jna=true 
-XX:+HeapDumpOnOutOfMemoryError -Didea.paths.selector=AndroidStudioPreview1.4 -Didea.platform.prefix=AndroidStudio 
-Djb.vmOptions=C:\dev2\android-studio-1.4-beta2\bin\studio64.exe.vmoptions -Xbootclasspath/a:C:\dev2\android-studio-1.4-beta2\lib\boot.jar -Didea.jre.check=false

In a second step, we have to extract the process id using some simple string operations (searching for the first blank):

PS C:\Program Files\Java\jdk1.7.0_xx\bin> $javaActive = jps -v | select-string "-Didea.platform.prefix=AndroidStudio"
Write-host $javaActive.ToString().Substring(0,$javaActive.ToString().IndexOf(" "))

Kill Java applications

After we successfully got the process id, we now can kill a specific Java application (example Android Studio):

$javaActive = jps -v | select-string "-Didea.platform.prefix=AndroidStudio"
Stop-Process $javaActive.ToString().Substring(0,$javaActive.ToString().IndexOf(" "))

To kill all java applications do the following:

Stop-Process -processname *java*
Stop-Process -processname *jp2launcher*

(jp2launcher is needed for Java Web Start applications.)

Monitoring Java applications

If you want to constantly check if a specific Java application is running and store the result in the windows log, you should do the following.
Register a source in the Windows application log (run as admin):

New-EventLog -LogName Application -Source "My Application"

The checker script (example Android Studio):

$javaActive = jps -v
if($javaActive -like "*-Didea.platform.prefix=AndroidStudio*")
 Write-EventLog -LogName Application -Source "My Application" -EntryType Information -EventID 1  -Message "Application is running."
 Write-EventLog -LogName Application -Source "My Application" -EntryType Error -EventID 2  -Message "Application is not running."
 Optional: code to restart

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s