Example #1
0
        /// <summary>
        /// Searches for a Java executable in the specified home folder
        /// </summary>
        /// <param name="home">The path in which to search for a Java executable</param>
        /// <param name="flags">The search options to use to locate a Java installation</param>
        /// <returns>Information about the located Java installation</returns>
        public static Java GetJava(string home, JavaFlags flags, string version = null)
        {
            string JNAME = flags.HasFlag(JavaFlags.Legacy) && !flags.HasFlag(JavaFlags.Development) ? JRE : JDK; //the name of the Java installation
            string BTYPE = flags.HasFlag(JavaFlags.Development) ? JAVAC : JAVA;                                  //the name of the relevant Java executable

            Java output = new Java()
            {
                development = flags.HasFlag(JavaFlags.Development),
                is32bit     = flags.HasFlag(JavaFlags.Registry32),
                legacy      = flags.HasFlag(JavaFlags.Legacy),
                home        = home,
                version     = version
            }; //the output Java installation information

            //checks for the exe file in the path <home>/bin/
            string exe = Path.Combine(output.home, "bin", BTYPE);

            if (!File.Exists(exe))
            {
                output.error = $"Failed to locate {exe}.";
                if (!string.IsNullOrWhiteSpace(output.version))
                {
                    output.error += $" Try re-installing {JNAME} {output.version}.";
                }
                output.errtype = 6;
            }
            else
            {
                output.exe = exe;
            }

            return(output); //returns the located installation information
        }
Example #2
0
        /// <summary>
        /// Searches for any Java installation, preferring the specified search options
        /// </summary>
        /// <param name="flags">The search options to prefer when locate a Java installation</param>
        /// <param name="locked">The search options specified in the flags which must be used when locating a Java installation</param>
        /// <returns>Information about the current located installation</returns>
        public static Java SearchJava(JavaFlags flags = JavaFlags.Default, JavaFlags locked = JavaFlags.Default)
        {
            Java java = GetJava(flags);                            //try to find a Java installation with the default settings

            int[] vals = (int[])Enum.GetValues(typeof(JavaFlags)); //enumerate the flag values
            for (int i = 1; i < 1 << vals.Length; i++)             //for every combination of flag values...
            {
                if (java.errtype == 0)                             //if an installation has already been found, break
                {
                    break;
                }
                JavaFlags flags2 = flags;             //a modified set of search options
                for (int j = 0; j < vals.Length; j++) //for every flag
                {
                    //if the flag is not locked and is part of the current combination, toggle it
                    if (((int)locked & vals[j]) == 0 && (i & vals[j]) != 0)
                    {
                        flags2 ^= (JavaFlags)vals[j];
                    }
                }
                Java java2 = GetJava(flags2);                           //try to find a Java installation with the modified sttings
                if (java2.errtype == 0 || java2.errtype < java.errtype) //if the new search had better results, store it
                {
                    java = java2;
                }
            }
            return(java); //returns the located installation information
        }
Example #3
0
        /// <summary>
        /// Searches for the Java installation with the specified settings
        /// </summary>
        /// <param name="flags">The search options to use to locate a Java installation</param>
        /// <returns>Information about the located Java installation</returns>
        public static Java GetJava(JavaFlags flags)
        {
            string       JTYPE = flags.HasFlag(JavaFlags.Legacy) ? flags.HasFlag(JavaFlags.Development) ? JDK : JRE : SE; //the type of Java installation
            string       JNAME = flags.HasFlag(JavaFlags.Legacy) && !flags.HasFlag(JavaFlags.Development) ? JRE : JDK;    //the name of the Java installation
            RegistryView VIEW  = flags.HasFlag(JavaFlags.Registry32) ? RegistryView.Registry32 : RegistryView.Registry64; //the registry to search in

            Java output = new Java()
            {
                development = flags.HasFlag(JavaFlags.Development),
                is32bit     = flags.HasFlag(JavaFlags.Registry32),
                legacy      = flags.HasFlag(JavaFlags.Legacy)
            }; //the output Java installation information

            //check each property step-by-step, returning error information if the registry key can't be found
            //the output.error messages are sufficient as comments lol
            RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, VIEW);

            if (key == null)
            {
                output.error   = "Windows Registry is not available.";
                output.errtype = 1;
            }
            else
            {
                RegistryKey javakey = key.OpenSubKey($@"SOFTWARE\JavaSoft\{JTYPE}");
                if (javakey == null)
                {
                    output.error   = $"Your computer is missing a {JTYPE}. Try installing the latest version.";
                    output.errtype = 2;
                }
                else
                {
                    object version = javakey.GetValue("CurrentVersion");
                    if (version == null)
                    {
                        output.error   = $"Your computer has some {JTYPE} registry keys but none seem to be installed. Try re-installing a {JTYPE}.";
                        output.errtype = 3;
                    }
                    else
                    {
                        output.version = version.ToString();
                        RegistryKey verkey = javakey.OpenSubKey(output.version);
                        if (verkey == null)
                        {
                            output.error   = $"Java {output.version} is marked as your default {JTYPE} version but lacks a complete installation. Try re-installing {JTYPE} {output.version}.";
                            output.errtype = 4;
                        }
                        else
                        {
                            object home = verkey.GetValue("JavaHome");
                            if (home == null)
                            {
                                output.error   = $"Java {output.version} is marked as your default {JTYPE} version but lacks a JavaHome directory. Try re-installing {JTYPE} {output.version}.";
                                output.errtype = 5;
                            }
                            else
                            {
                                output = GetJava(home.ToString(), flags, output.version);
                            }
                        }
                    }
                }
            }

            return(output); //returns the located installation information
        }