How can I find the program code for javac.exe?

I am calling javac from C # code. I originally found its location just like this:

protected static string JavaHome
{
    get
    {
        return Environment.GetEnvironmentVariable("JAVA_HOME");
    }
}

      

However, I just installed the JDK on a new computer and found that it did not automatically set the JAVA_HOME environment variable. Requiring an environment variable is not acceptable in any Windows application over the past decade, so I need to find javac if JAVA_HOME environment variable is not set:

protected static string JavaHome
{
    get
    {
        string home = Environment.GetEnvironmentVariable("JAVA_HOME");
        if (string.IsNullOrEmpty(home) || !Directory.Exists(home))
        {
            // TODO: find the JDK home directory some other way.
        }

        return home;
    }
}

      

+2


source to share


3 answers


If you're on Windows, use the registry:

HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft \ Java Development Kit

If you don't, you are pretty much stuck with env variables. You can find this blog post.



Edited 280Z28:

This registry key refers to the CurrentVersion value. This value is used to find Java home at the following location:
HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\{CurrentVersion}\JavaHome

private static string javaHome;

protected static string JavaHome
{
    get
    {
        string home = javaHome;
        if (home == null)
        {
            home = Environment.GetEnvironmentVariable("JAVA_HOME");
            if (string.IsNullOrEmpty(home) || !Directory.Exists(home))
            {
                home = CheckForJavaHome(Registry.CurrentUser);
                if (home == null)
                    home = CheckForJavaHome(Registry.LocalMachine);
            }

            if (home != null && !Directory.Exists(home))
                home = null;

            javaHome = home;
        }

        return home;
    }
}

protected static string CheckForJavaHome(RegistryKey key)
{
    using (RegistryKey subkey = key.OpenSubKey(@"SOFTWARE\JavaSoft\Java Development Kit"))
    {
        if (subkey == null)
            return null;

        object value = subkey.GetValue("CurrentVersion", null, RegistryValueOptions.None);
        if (value != null)
        {
            using (RegistryKey currentHomeKey = subkey.OpenSubKey(value.ToString()))
            {
                if (currentHomeKey == null)
                    return null;

                value = currentHomeKey.GetValue("JavaHome", null, RegistryValueOptions.None);
                if (value != null)
                    return value.ToString();
            }
        }
    }

    return null;
}

      

+4


source


You should probably look in the registry for the JDK installation address.



Alternatively see this discussion.

+1


source


For 64-bit OS (Windows 7), the registry key may be located under

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit

if you are using 32bit JDK. So, if you've all written code based on the above, repeat the test.

I don't have the whole Microsoft registry redirector / reflection around yet .

0


source







All Articles