Find PID Java Process

Find PID Java Process

We all knows how to write program in Java but sometime while working on the project we need to find PID of running application for various use for example: Kill the existing Java Process, Check how much it’s consuming etc… Below is sample Java prgram which will find the PID of running Java application:

  • FindPIDJavaProces.java:
import java.io.File;
import java.nio.charset.Charset;

import org.apache.commons.io.FileUtils;

public class FindPIDJavaProces {
	
	public static void main(String[] args) {
		writePid();
	}
	
	public static void writePid() {

		try {
			String processName = java.lang.management.ManagementFactory
					.getRuntimeMXBean().getName();
			Long pid = Long.parseLong(processName.split("@")[0]);
			FileUtils.writeStringToFile(new File("C:\\Users\\JavaHonk\\PID.text"), pid.toString(),
					Charset.defaultCharset());
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

} 
  • YOu can also find runing Java PID using Window task manager –> Right click program bar –> Strat task manager –> View –> Select columns –> Choose PID (Process Identifier)

Find PID Java Process

 

  • To kill Java PID from command prompt use below:

TASKKILL /PID %PID%

Leave a Reply

Your email address will not be published. Required fields are marked *