def get_java_home():
error = RuntimeError("<STR_LIT>")<EOL>try:<EOL><INDENT>return os.environ["<STR_LIT>"]<EOL><DEDENT>except KeyError:<EOL><INDENT>wd = tempfile.mkdtemp(prefix='<STR_LIT>')<EOL>jclass = "<STR_LIT>"<EOL>jsrc = os.path.join(wd, "<STR_LIT>" % jclass)<EOL>with open(jsrc, "<STR_LIT:w>") as f:<EOL><INDENT>f.write(JPROG.substitute(classname=jclass))<EOL><DEDENT>try:<EOL><INDENT>subprocess.check_call(["<STR_LIT>", jsrc])<EOL>path = subprocess.check_output(<EOL>["<STR_LIT>", "<STR_LIT>", wd, jclass], universal_newlines=True<EOL>)<EOL><DEDENT>except (OSError, UnicodeDecodeError, subprocess.CalledProcessError):<EOL><INDENT>raise error<EOL><DEDENT>finally:<EOL><INDENT>shutil.rmtree(wd)<EOL><DEDENT>path = os.path.normpath(path.strip())<EOL>if os.path.exists(os.path.join(path, "<STR_LIT>", "<STR_LIT>")):<EOL><INDENT>return path<EOL><DEDENT>path = os.path.dirname(path)<EOL>if os.path.exists(os.path.join(path, "<STR_LIT>", "<STR_LIT>")):<EOL><INDENT>return path<EOL><DEDENT>raise error<EOL><DEDENT>
\
    Try getting JAVA_HOME from system properties.

    We are interested in the JDK home, containing include/jni.h, while the
    java.home property points to the JRE home. If a JDK is installed, however,
    the two are (usually) related: the JDK home is either the same directory
    as the JRE home (recent java versions) or its parent (and java.home points
    to jdk_home/jre).