Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.util.StringTokenizer;

public class Main {
    public final static String SHELL_CMD_PS = "ps";

    public static int findProcessIdWithPS(String command) throws Exception {
        int procId = -1;
        Runtime r = Runtime.getRuntime();
        Process procPs = null;
        procPs = r.exec(SHELL_CMD_PS);

        BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
        String line = null;

        while ((line = reader.readLine()) != null) {
            if (line.indexOf(' ' + command) != -1) {
                StringTokenizer st = new StringTokenizer(line, " ");
                st.nextToken(); // proc owner
                procId = Integer.parseInt(st.nextToken().trim());
                break;
            }
        }

        return procId;
    }
}