문제

C# 코드에서 Javac을 호출합니다. 원래 위치를 다음과 같이 찾았습니다.

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

그러나 방금 새 컴퓨터에 JDK를 설치 한 결과 Java_home 환경 변수를 자동으로 설정하지 않았다는 것을 알았습니다. 요구 지난 10 년간 Windows 애플리케이션에서는 환경 변수가 용납 할 수 없으므로 Java_home 환경 변수가 설정되지 않은 경우 Javac을 찾는 방법이 필요합니다.

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;
    }
}
도움이 되었습니까?

해결책

Windows에있는 경우 레지스트리를 사용하십시오.

hkey_local_machine 소프트웨어 javasoft java 개발 키트

당신이 그렇지 않다면, 당신은 ENV 변수에 거의 붙어 있습니다. 당신은 찾을 수 있습니다 이것 블로그 항목이 유용합니다.

280Z28에 의해 편집 :

해당 레지스트리 키 아래에는 CurrentVersion 값이 있습니다. 그 가치는 다음 위치에서 Java 집을 찾는 데 사용됩니다.
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;
}

다른 팁

JDK 설치 주소에 대한 레지스트리를 검색해야 할 것입니다.

대안으로, 참조하십시오 이것 논의.

64 비트 OS (Windows 7)의 경우 레지스트리 키가 아래에있을 수 있습니다.

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit

32 비트 JDK를 실행하는 경우 따라서 위의 내용을 기준으로 모든 코드를 작성한 경우 다시 테스트하십시오.

나는 아직 내 머리를 완전히 없애지 않았다 Microsoft 레지스트리 리디렉션/반사 아직.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top