is Process Running on Windows - Java java.lang

Java examples for java.lang:Process

Description

is Process Running on Windows

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Main {
    public static void main(String[] argv) throws Exception {
        String processName = "java2s.com";
        System.out.println(isProcessRunning(processName));
    }//from   w w w  .  j  a v a2 s  . c  om

    public static boolean isProcessRunning(String processName)
            throws IOException {
        ProcessBuilder processBuilder = new ProcessBuilder("tasklist");
        Process process = processBuilder.start();
        String tasksList = toString(process.getInputStream());

        return tasksList.contains(processName);
    }

    private static String toString(InputStream inputStream) {
        Scanner scanner = new Scanner(inputStream, "UTF-8")
                .useDelimiter("\\A");
        String string = scanner.hasNext() ? scanner.next() : "";
        scanner.close();

        return string;
    }
}

Related Tutorials