2 Replies Latest reply: Mar 17, 2009 9:44 AM by 807588 RSS

    Batch File That Starts Java VM Exits - Java App Still Running

      I have a Windows batch file running on Windows Server 2003 RC2, that starts a small command line utility written in Java 6. Occasionally I need to kill the Windows shell ( cmd.exe ) process to change some parameters and restart the process.

      However, 90% of the time the shell closes but the java process continues to run, its usually actively writing to a file when I kill the Windows shell . Its there any way to tie the java process to the shell or am I SOL? It would be nice not to have to manually log into the remote system to kill the java.exe process that represents the java application, but right now I need to do that.

      Btw, its remote because a use a process scheduler to kick off the shell that then starts the java application. When I tell the schedule to kill the process, it kills the shell, but not the java process, as explained above.

      Thank You,

        • 1. Re: Batch File That Starts Java VM Exits - Java App Still Running
          I suppose one alternative might be to make a way of sending a message to the Java process directly, and then let the fallout of the Java process end the parent process. Something like this might work:
          public class SomeClass {
             private static final int myPort = 5678;
             public static void main(String[] args) throws Exception {
                Thread mainWorker = new Thread( new Runnable() {
                   public void run() {
                      for(;;) {
                         //do something with file I/O
                         if ( false ) {
                      System.exit(0); //normal exit: processing finishes
                ServerSocket sck = new ServerSocket(myPort);
          With this code, simply telnetting to port 5678 at that computer's IP will cause the Java process to exit, which should cause the parent process to execute as well. This of course would need to be cleared by the firewall.
          • 2. Re: Batch File That Starts Java VM Exits - Java App Still Running
            Not a bad idea. This process is likely to be scheduled to run every 10minutes, the problem being if it doesn't exit when it should, there is really no way to know when to kill it. It its running, it may very well be that is should be running, and therefore should not be killed. Or, its possible that it's running when it shouldn't be, and therefore the telnet idea you mentioned should be executed.

            However, since this process will be running 24/7 every 10 minutes, there definitely needs to be a completed automated way to terminate the process when necessary.

            So, we need to know:
            1) When it is necessary
            2) Which java.exe process to kill, b/c an invalid one may still be running when a valid one begins.
            3) How to terminate it properly.

            If we can just have the java.exe process terminate properly when it has done its job, then steps 1,2,3, are obsolete, which is the way I would like to go.

            Thank You,