0

I am writing a program to kill and start processes in another computer connected via lan. I am able to kill the process successfully. I used tasklist to list the process and taskkill for killing it. To start the killed program again the path of the process have to be obtained.

Is there a way that I can do it in java?

Erwin
  • 4,757
  • 3
  • 31
  • 41

1 Answers1

0

I don't think it's possible to kill a process by it's path, but you can always kill it by either it's process name or id.

Here's what I use to kill a process, e.g. firefox.exe.

  1. Create a VB script as follows,

    sub killProcess(strProcessName)
        set colProcesses = GetObject("winmgmts:\\.\root\cimv2").ExecQuery("Select * from Win32_Process Where Name='" & strProcessName & "'")
        if colProcesses.count <> 0 then
            for each objProcess in colProcesses
                objProcess.Terminate()
            next
        end if
    end sub
    
    killProcess "firefox.exe"
    
  2. Now in order to run the above script via Java, use the Process API as follows,

    Process pr = Runtime.getRuntime().exec(path_to_vbscript_file, null, null);   
    pr.waitFor();
    
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
Arham
  • 2,072
  • 2
  • 18
  • 30