954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Killing Java Process when App Ends

I wrote a small c# .NET application that is basically a controller for a Java application. My company has been using a .bat file to launch it, but we need an .exe so our web panel can detect when it is online/offline/etc. When my .NET application starts, it starts the Java app, when my .NET application ends, it SHOULD kill the Java app.

For some reason, it is not killing the Java application. If anyone can take a look at my code and let me know if they see the reason (and also if theres anything I could improve as this is my first C# .NET app)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using CommandLine.Utility;

namespace MinecraftDaemon
{
    class Program
    {
        public static void LaunchMinecraft(String file, String memoryValue)
        {
            String memParams = "-Xmx" + memoryValue + "M" + " -Xms" + memoryValue + "M ";
            String args = memParams + "-jar " + file + " nogui";
            ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", args);
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;

            try
            {
                using (Process minecraftProcess = Process.Start(processInfo))
                {
                    GlobalClass.ProcessID = minecraftProcess.Id;
                    Console.WriteLine("Process ID is " + GlobalClass.ProcessID);
                    minecraftProcess.WaitForExit();
                }
            }
            catch
            {
                // Log Error
            }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Starting Minecraft Daemon...");

            Arguments CommandLine = new Arguments(args);

            // Hook ProcessExit Event
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(Current_ProcessExit);

            if (CommandLine["file"] != null && CommandLine["memory"] != null)
            {
                // Launch the Application (Command Line Parameters)
                LaunchMinecraft(CommandLine["file"], CommandLine["memory"]);
            }
            else
            {
                // Launch the Application (Default Parameters)
                LaunchMinecraft("minecraft_server.jar", "1024");
            }
        }

        static void Current_ProcessExit(object sender, EventArgs e)
        {
            // Loop the Current Windows Processes
            foreach (Process winProcess in Process.GetProcesses())
            {
                Console.WriteLine("WinProcessID is " + winProcess.Id + " GlobalClass.ProcessID is " + GlobalClass.ProcessID);

                // If this is our Process, shut it down
                if (winProcess.Id == GlobalClass.ProcessID)
                {
                    Process.GetProcessById(GlobalClass.ProcessID).Kill();
                }
            }
        }
    }
}
CrimsonGT
Newbie Poster
4 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: