a.
Using jps command
‘jps’
command return the Process id followed by application name.
Test.java
import java.util.concurrent.TimeUnit; public class Test { public static void main(String args[]) throws InterruptedException { while(true) { TimeUnit.MINUTES.sleep(15); } } }
Run Test.java.
Open
another terminal and execute the command ‘jps’, you can see the all the java
process details.
$ jps 86817 Jps 83984 WrapperSimpleApp 77527 81160 BootLanguagServerBootApp 81535 jar 86815 Test 80926
b. Programmatically
Below
statements return the process id of java application.
RuntimeMXBean
bean = ManagementFactory.getRuntimeMXBean();
String
beanName = bean.getName();
long pid =
Long.valueOf(beanName.split("@")[0]);
Test.java
import java.lang.management.ManagementFactory; import java.lang.management.RuntimeMXBean; import java.util.concurrent.TimeUnit; public class Test { public static void main(String args[]) throws InterruptedException { RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean(); String beanName = bean.getName(); long pid = Long.valueOf(beanName.split("@")[0]); System.out.println("PID = " + pid); while (true) { TimeUnit.MINUTES.sleep(15); } } }
Run
Test.java, it prints the process id of the application.
$javac
Test.java
$java Test
PID = 86830
Open another
terminal or command prompt and execute the command ‘jps’, and confirm the
process id of the application ‘Test.java’.
$ jps 83984 WrapperSimpleApp 86832 Jps 77527 81160 BootLanguagServerBootApp 81535 jar 80926 86830 Test
No comments:
Post a Comment