0

I need to get process ID of running task. I am not sure why below code is failing. It is going unresponsive everytime i run the code as standalone.

 Map<String,String> checkRunningProcess(){
        Map<String,String>processPid = new HashMap<String,String>();
        try{
            String line,processName;
            Process p = Runtime.getRuntime().exec("tasklist.exe /fo csv /nh"); 
            BufferedReader input =  new BufferedReader (new InputStreamReader(p.getInputStream()));


            while((line = input.readLine())!=null){
                String all[] = line.split(",");
                all[0] = all[0].replace("\"","");
                all[1] = all[1].replace("\"", "");
                //System.out.println(all[0]+all[1]);

                  processPid.put(all[0],all[1]);
            }

        }catch(Exception err){
            err.printStackTrace();
        }
        return processPid;
    }
user2093576
  • 3,082
  • 7
  • 32
  • 43
  • You should run the code using a debugger and see what output you are getting from the provess. From that you can narrow down which of the possible problems this could be. – Stephen C May 15 '17 at 10:48
  • Someone asked in a (non-)answer what the OS and Java versions are. The OS is clearly Windows, but you could tell us what the Java version is. (I don't actually think it is relevant ... but someone does.) – Stephen C May 15 '17 at 11:15
  • 1
    How do you run it? When I ran your method from a main method, it simply worked fine. – Ossin Java guy May 15 '17 at 11:19
  • Also, can you manually run the command via the command line console? – Ossin Java guy May 15 '17 at 11:20
  • I wonder if on some versions of Windows, `tasklist.exe` outputs to stderr instead of stdout? – Stephen C May 15 '17 at 11:57
  • Do you want the PID of the current running Java application? – SubOptimal May 15 '17 at 12:12

0 Answers0