How to kill the running .jar file from another jar in java?

There are two jars running A.jar & B.jar
Both jars are running.
I want to kill B.jar from A.jar.
Pls let me know, if any one knows.

Thanks in advance.

It all depends on what you are doing.

If you are starting B.jar from A.jar as a new process, it's quite simple. Just kill the process. If you launch the two programs seperately from the OS, I don't think you can do it in plain java. I think you can write som JNI code to interface with the OS and get the process ID for the other program and ask the OS to kill it, but it would only work on the OS you are writing JNI code for.

You could always use IPC (Inter-process communication) where B.jar listens to a socket, and when A.jar signals B.jar it gracefully shuts down.

Maybe there's a twisted way you can interface with the JVM and ask it to shut down the other process, but I doubt it.

Hope this helps

-vidaj-

It all depends on what you are doing.

If you are starting B.jar from A.jar as a new process, it's quite simple. Just kill the process. If you launch the two programs seperately from the OS, I don't think you can do it in plain java. I think you can write som JNI code to interface with the OS and get the process ID for the other program and ask the OS to kill it, but it would only work on the OS you are writing JNI code for.

You could always use IPC (Inter-process communication) where B.jar listens to a socket, and when A.jar signals B.jar it gracefully shuts down.

Maybe there's a twisted way you can interface with the JVM and ask it to shut down the other process, but I doubt it.

Hope this helps

-vidaj-

Thanks Vidaj,

yes,I m starting B.jar from A.jar.In such case what I need to do?

In java 1.5 they introduced the ProcessBuilder which you can use.

void jarStarter() {
    String jarFile = "B.jar";
    try {
        /* Start the jar-file. */
        final Process process startJar(jarFile, true);

        /* Get the file's output. */
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;

        /* Start reading output and print it to console. If we don't read the programs output, it won't function properly. You don't have to print it out again, but you have to read it. */
        while ((line = br.readLine()) != null) {
            System.out.println(line);

            /* Kill the other process whenever you want to :) The only thing you need in order to kill it, is the process-object and calling the #destroy-method on it. */
            if (line.equals("LOADING. DO NOT TERMINATE PROCESS OR COMPUTER.")) {
                 process.destroy();
            }
        }
   
        /* When the output becomes null, the program terminated itself. */
        System.out.println("Program terminated!");
    } catch (InterruptedException e) {
          e.printStackTrace();
    } catch (IOException e) {
          e.printStackTrace();
    }
    
}

Process startJar(String jarFile, boolean mergeStreams) throws InterruptedException,IOException  {

    /* Build the command to be executed. */
    List<String> command = new ArrayList<String>();
    command.add("java");
    command.add("-jar");
    command.add(jarFile);

    ProcessBuilder builder = new ProcessBuilder(command);
    Map<String, String> environ = builder.environment();
    builder.directory(new File(System.getenv("temp")));

    /* Merge the error-stream into the standard output-stream. */
    if (mergeStreams) {
        builder.redirectErrorStream();
    }

    /* Start the process. */
    final Process process = builder.start();
    
    return process;
  }
}

Mind that I wrote the code directly in this window, so it probably won't compile, but you'll probably figure out the bugs. The code is rewritten from the code taken from http://www.rgagnon.com/javadetails/java-0014.html

Just read the java API for ProcessBuilder and Process and you'll be fine :)

-vidaj-

Thanks for the effort, vidaj.

I will try this & let u know.
I too want to know whether B.jar is running or not? because, there is an option in B.jar to exit from itself.

I'm guessing that you have the sourcecode for B.jar.

The first thing that the program does is to start a new thread that listens to a socket. This thread won't stop (hopefully) until the program stops executing. Whenever it detects an incoming connection on the listening port, just send a response back and terminate the connection. Rince and repeat :)

So before you call B.jar, you try to send a message on the socket. If there's no answer, start B.jar. If there is an answer, you know it's already started :)

Depending on what messages you send, you can implement different behaviour in B.jar. I.e. you can send "QUIT" and when it receives a string with that content, it terminates.

Take a look at http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html for more information on how to code a client/server pair. The server-code goes into B.jar and the client code goes into A.jar before you start the new process.

-vidaj-

Hi vidaj,

Thanks a lot,
Finally, I make it with ur help.I did with socket communication.

Its quite hard to know abt IPC and Processbuilder.Since ,I can't find any ProcessBuilder with the java version 1.4.2_13, I m using.I think its necessary to know abt IPC, pls refer me any tutorial for this to get better idea.

Good post...

Please suggest how this can be achieved and best approach to follow.

How can we exceute jar on different machine with memory args
Example:
a) Suppose server1 have application which is started using "java -Xmx512M -jar <app1>.jar" from command prompt from particular directory example C:\proj\application1>
Based on certain condition I want to stop this <app1>.jar on server1 and

b) Start app2.jar on server2 using "java -Xmx4G -jar <app2>.jar" from particular directory example C:\proj\application2> (we need to take care of memory here somehow)

c) once <app2>.jar finishes some processing on server2 then stop <app2>.jar and start "<app1>.jar" on server1

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.