Pergunta

Eu estou chamando javac de código C #. Originalmente eu achei a sua localização apenas como segue:

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

No entanto, Acabei de instalar o JDK em um novo computador e descobriu que ele não define automaticamente a variável de ambiente JAVA_HOME. Exigindo uma variável de ambiente é inaceitável em qualquer aplicativo Windows para a última década, então eu preciso de uma maneira de encontrar javac se a variável de ambiente JAVA_HOME não está definido:

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;
    }
}
Foi útil?

Solução

Se você estiver no Windows, registro do uso:

HKEY_LOCAL_MACHINE \ SOFTWARE \ JavaSoft \ Java Development Kit

Se você não for, você está muito presa com variáveis ??env. Você pode achar neste blog entrada útil.

Editado por 280Z28:

Sob essa chave Registro é um valor CurrentVersion. Esse valor é usado para encontrar a casa Java no seguinte local:
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;
}

Outras dicas

Você provavelmente deve procurar o registro para um endereço de instalação do JDK.

Como alternativa, consulte esta discussão .

Para 64-bit OS (Windows 7), a chave de registro pode estar sob

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit

Se você estiver executando um 32-bit JDK. Então, se você tem todo o código escrito com base no acima exposto, ir teste novamente.

Eu ainda não tenho a minha cabeça completamente em torno do Microsoft registro redirecionamento / reflexão coisas ainda.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top