-2

I want to stop a Java program that has been started using the command prompt in Windows.

How is it possible to shut it down using some other running Java program?

rghome
  • 8,529
  • 8
  • 43
  • 62
SSD
  • 359
  • 1
  • 3
  • 15
  • Do you want it to stop itself or another java program? – Laurel May 12 '16 at 16:35
  • @Laurel of course I mean by another java program. I wrote "This should be with a java program not manually." – SSD May 12 '16 at 16:37
  • http://stackoverflow.com/questions/4409731/how-can-i-programatically-close-a-specific-java-application-running-in-windows-f – Idos May 12 '16 at 16:46
  • Previously asked and answered [here](http://stackoverflow.com/questions/6356340/killing-a-process-using-java). – Jeff W May 12 '16 at 18:15
  • Thanks @Jeff W however that answer does not say anything about how to get PID of another process. I used "jps" to get all running java processes. – SSD May 12 '16 at 18:25
  • Process proc = runtime.exec("cmd.exe /k \"C:\\Program Files\\Java\\jdk1.8.0_73\\bin\\jps.exe\""); InputStream inputstream = proc.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); String line; while ((line = bufferedreader.readLine()) != null) { System.out.println(line); } – SSD May 12 '16 at 18:26
  • however, this seems an ugly solution. If you have better solution I do appreciate you – SSD May 12 '16 at 18:27
  • @rghome May I ask you please to up vote my question. I cannot ask any more question. – SSD Jun 01 '16 at 22:10
  • @SSD have you looked at the other linked answers? This question seems likely to be a duplicate. You can always delete it and get your points back. – rghome Jun 01 '16 at 22:38
  • @rghome unfortunately because it has a answer I cannot delete the question. I wanted but I could not. If possible please delete the question. – SSD Jun 02 '16 at 00:58

1 Answers1

1

If you want to program to terminate or stop, just type in

System.exit(0);

Although System.exit(1); will indicate that a "non-zero" exit code occurred which usually means something wrong happened. User 0 if it exits and everything is fine. Use non-zero (like 1) to indicate that there was an error (like a non-existent file or something).

If you want to stop the java program from another java program, you first have to get the process id of the java program. Once this process ID is known, you can kill it with a system call (just like you would in a terminal).

For example, if I had this Java program running indefinitly:

import java.lang.management.ManagementFactory;
public class Test {
    public static void main(String[] args) {
        System.out.println("Hello, World");
        System.out.println(ManagementFactory.getRuntimeMXBean().getName());
        while (true) {
                int i = 0;
        }   
    }   
}

I could see it's process ID (for example 123) and kill it with the following command.

$ kill 123 

Once this is done, you have to find some way for it to output this data to another java program after it asks for it. Then once the process ID is known, you would administer a system call or shell exec.

import java.lang.* ;

public class Shell 
{
        public static void main(String[] args)
        {   
                try { 
                        String cmd = "kill 123";
                        Runtime run = Runtime.getRuntime();
                        Process pr = run.exec(cmd) ;
                } catch (Exception ex) {
                        System.out.println("SOMETHING WENT WRONG");
                }   
        }   
}

Node that all shell commands or run.exec must catch the exception since it is thrown. It must be in a try catch block or it won't compile.

Hope this helps.

Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111
  • Thanks. but my problem is I have a java program suppose A that is running and I want to kill that program by another java program B. – SSD May 12 '16 at 17:19
  • Check me new edits. Let me know if that works for you. – Alexander Kleinhans May 12 '16 at 17:36
  • In windows, instead of `kill`, you need to use `Taskkill` on windows shell: https://technet.microsoft.com/en-us/library/bb491009.aspx – Alexander Kleinhans May 12 '16 at 17:40
  • Thank you very much. My problem now is how to get PID of that program. I know that jps included in JRE shows the running java processes with their PID. However I do not how to call it within the program? May I call it with exec and get inputstream of it? – SSD May 12 '16 at 17:44
  • Yes I check if it is Linux or windows to run kill in linux or taskkill in windows. – SSD May 12 '16 at 17:45
  • May I ask you please to up vote my question. I cannot ask any more question. – SSD Jun 01 '16 at 22:08