CmlLib.Core icon indicating copy to clipboard operation
CmlLib.Core copied to clipboard

Download Minecraft JAR File from URL and run it.

Open EnergixCode opened this issue 3 years ago • 2 comments

If I have a Minecraft .JAR file and .JSON file located at 2 different urls, I was wondering if I could run it using a method. I've tried downloading it, storing it to a folder but I don't know how I would run it.

Describe the solution you'd like I would want like a way to run it using CmlLib as this would be really useful for Auto Updaters as then they could get their jar file from a link and immediately run it.

Describe alternatives you've considered I couldnt think of any alternative.

Additional context I'm trying to make a launcher for a Minecraft PvP Client, I have a link to the JAR file and JSON file but I dont know how to run it.

EnergixCode avatar Nov 21 '21 19:11 EnergixCode

If I understand correctly, the goal is to update the launcher ?

I can propose you another method in C# using this NuGet package : https://github.com/ravibpatel/AutoUpdater.NET

Otherwise, to execute a java app inside c# app : (i don't recommend that)

        private void DownloadAndStart(string version, string build) 
        {
            WebClient wc = new WebClient(); // First download the .jar file 
            wc.DownloadFileAsync(
                    // Param1 = Link of file
                    new System.Uri("LINKTOTHEJAR"),
                    // Param2 = Path to save
                    path + "\\mods"  /: here for exmeple .minecraft/mods (path = path to .minecraft)
                );
            System.Threading.Thread.Sleep(10000); // Wait 10 s SHOULD change this with an event handler to get the download finish event

            ProcessStartInfo ps = new ProcessStartInfo(GetJavaInstallationPath() + "\\bin\\java.exe", @"-jar PATH+FILE.jar"; // Exclusive to Windows due to .exe

            Process p = Process.Start(ps);
            p.WaitForExit();

        }


        private string GetJavaInstallationPath() // get the Javapath
        {
            string environmentPath = Environment.GetEnvironmentVariable("JAVA_HOME");
            if (!string.IsNullOrEmpty(environmentPath))
            {
                return environmentPath;
            }

            string javaKey = "SOFTWARE\\JavaSoft\\Java Runtime Environment\\";
            using (Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(javaKey))
            {
                string currentVersion = rk.GetValue("CurrentVersion").ToString();
                using (Microsoft.Win32.RegistryKey key = rk.OpenSubKey(currentVersion))
                {
                    return key.GetValue("JavaHome").ToString();
                }
            }
        }

Hope that help

DaFray31 avatar Nov 22 '21 09:11 DaFray31

That looks quite interesting, I'll have to try that and get back to you.

EnergixCode avatar Dec 04 '21 08:12 EnergixCode