How do I get process id of a Java application?

The code below show you how to get the process id of a Java application. We can use the ManagementFactory.getRuntimeMXBean().getName() to get the process id. In Windows the method return a string in the form of [PID]@[MACHINE_NAME].

package org.kodejava.example.management;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;

public class GetProcessID {
    public static void main(String[] args) {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();

        // Get name representing the running Java virtual machine.
        // It returns something like 6460@AURORA. Where the value
        // before the @ symbol is the PID.
        String jvmName = bean.getName();
        System.out.println("Name = " + jvmName);

        // Extract the PID by splitting the string returned by the
        // bean.getName() method.
        long pid = Long.valueOf(jvmName.split("@")[0]);
        System.out.println("PID  = " + pid);
    }
}

Here is the result of the code above:

Name = 8564@AURORA
PID  = 8564

How do I execute external command and obtain the result?

This example demonstrate how to execute an external command from Java and obtain the result of the command. Here we simple execute a Linux ls -al command on the current working directory and display the result.

package org.kodejava.example.lang;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProcessResult {
    public static void main(String[] args) {
        try {
            Process process = Runtime.getRuntime().exec("ls -al");

            // Wait for this process to finish or terminated
            process.waitFor();

            // Get process exit value
            int exitValue = process.exitValue();
            System.out.println("exitValue = " + exitValue);

            // Read the result of the ls -al command by reading the
            // process's input stream
            InputStreamReader is = new InputStreamReader(process.getInputStream());
            BufferedReader reader = new BufferedReader(is);
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Below is our program result:

exitValue = 0
total 64
drwxr-xr-x  20 wsaryada  staff   680 Aug  7 14:13 .
drwxr-xr-x   4 wsaryada  staff   136 Jun 18 00:33 ..
-rw-r--r--@  1 wsaryada  staff  6148 Jun 29 14:04 .DS_Store
-rw-r--r--   1 wsaryada  staff   267 May 19 20:47 .editorconfig
drwxr-xr-x  16 wsaryada  staff   544 Aug 10 08:34 .git
-rw-r--r--   1 wsaryada  staff  1535 May 19 20:47 .gitignore
drwxr-xr-x  15 wsaryada  staff   510 Aug 10 08:34 .idea
-rw-r--r--   1 wsaryada  staff  1313 May 19 20:47 LICENSE
-rw-r--r--   1 wsaryada  staff   101 May 19 20:47 README.md
drwxr-xr-x   5 wsaryada  staff   170 Jul 28 23:11 kodejava-basic
drwxr-xr-x   6 wsaryada  staff   204 Jun 30 14:22 kodejava-commons
drwxr-xr-x   6 wsaryada  staff   204 Jul 27 15:32 kodejava-lang-package
drwxr-xr-x@  5 wsaryada  staff   170 Jun 16 20:49 kodejava-mail
drwxr-xr-x   6 wsaryada  staff   204 Aug  7 17:22 kodejava-mybatis
drwxr-xr-x   5 wsaryada  staff   170 Jul 20 10:36 kodejava-poi
-rw-r--r--   1 wsaryada  staff   669 Jun  2 14:29 kodejava-project.iml
drwxr-xr-x   7 wsaryada  staff   238 Jun 26 21:52 kodejava-swing
drwxr-xr-x   6 wsaryada  staff   204 Aug  3 15:06 kodejava-util-package
drwxr-xr-x   6 wsaryada  staff   204 Jun 30 15:09 maven-helloworld
-rw-r--r--   1 wsaryada  staff  1622 Aug  7 14:13 pom.xml

How do I execute other applications from Java?

The following program allows you to run / execute other application from Java using the Runtime.exec() method.

package org.kodejava.example.lang;

import java.io.IOException;

public class RuntimeExec {
    public static void main(String[] args) {
        //String command = "/Applications/Safari.app/Contents/MacOS/Safari";
        String command = "explorer.exe";

        try {
            Process process = Runtime.getRuntime().exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}