You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pylucene-dev@lucene.apache.org by Christian Heimes <li...@cheimes.de> on 2012/05/08 21:44:53 UTC
Easy way to find JAVA_HOME
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()
---
Re: Easy way to find JAVA_HOME
Posted by Andi Vajda <va...@apache.org>.
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 <ch...@cheimes.de> 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 <li...@cheimes.de>
> Antwort an: pylucene-dev@lucene.apache.org
> An: pylucene-dev@lucene.apache.org
>
> 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()
> ---