Home | About | Sematext search-lucene.com search-hadoop.com
 Search Lucene and all its subprojects:

Switch to Threaded View
PyLucene, mail # dev - Re: Easy way to find JAVA_HOME


Copy link to this message
-
Re: Easy way to find JAVA_HOME
Andi Vajda 2012-06-20, 14:45
Please, send your question to the list, instead of me directly. I remember seeing your message originally and thinking to myself "what a hack, parsing this message, there's got to be a more deterministic way". Having seen quite a few ways to lay out Java on Linux, I also had zero confidence that this would cover all cases anyway. Is the definition of JAVA_HOME that it is the parent dir of 'jre' ? If so, where is that stated. ?
Thus my silence, hoping someone on the list would chime in with a counter-example.

Andi..

On Jun 20, 2012, at 4:03, Christian Heimes <[EMAIL PROTECTED]> wrote:

> Hi Andi,
>
> I posted the suggestion over a month ago and didn't hear back from you.
> Do you like my idea?
>
> Christian
>
> -------- Original-Nachricht --------
> Betreff: Easy way to find JAVA_HOME
> Datum: Tue, 08 May 2012 21:44:53 +0200
> Von: Christian Heimes <[EMAIL PROTECTED]>
> Antwort an: [EMAIL PROTECTED]
> An: [EMAIL PROTECTED]
>
> Hello,
>
> I found a much easier to detect the path to JAVA_HOME on Unix-like
> platforms where the java command is in the search path. "java -verbose"
> prints out the paths of all loaded JAR files.
>
> Christian
>
> ---
> import subprocess
> import re
> import os
>
> PATH_RE = re.compile("Loaded\ .*\ from\ (.*)/jre/lib/rt.jar")
>
> def find_java():
>    """Find java home by running java -verbose
>
>    In verbose mode, java prints lines like
>
>        [Loaded java.lang.Object from
> /usr/lib/jvm/java-6-openjdk-amd64/jre/lib/rt.jar]
>
>    to stdout.
>    """
>    try:
>        proc = subprocess.Popen(["java", "-verbose"],
>                                stdout=subprocess.PIPE)
>    except OSError:
>        return None
>    out, err = proc.communicate()
>    for line in out.split("\n"):
>        mo = PATH_RE.search(line)
>        if mo is None:
>            continue
>        javahome = mo.group(1)
>        if os.path.isdir(javahome):
>            return javahome
>
> if __name__ == "__main__":
>    print find_java()
> ---