Skip to content
This repository was archived by the owner on Aug 5, 2021. It is now read-only.

Commit 64b6585

Browse files
committed
adding autodetection of java home
1 parent 33324da commit 64b6585

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

jpyutil.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import ctypes
3030
import ctypes.util
3131
import logging
32+
import subprocess
3233

3334

3435
__author__ = "Norman Fomferra, Brockmann Consult GmbH"
@@ -115,7 +116,8 @@ def _get_java_api_properties(fail=False):
115116

116117
def find_jdk_home_dir():
117118
"""
118-
Try to detect the JDK home directory from dedicated environment variables.
119+
Try to detect the JDK home directory from Maven, if available, or use
120+
dedicated environment variables.
119121
:return: pathname if found, else None
120122
"""
121123
for name in JDK_HOME_VARS:
@@ -124,6 +126,22 @@ def find_jdk_home_dir():
124126
and os.path.exists(os.path.join(jdk_home_dir, 'include')) \
125127
and os.path.exists(os.path.join(jdk_home_dir, 'lib')):
126128
return jdk_home_dir
129+
logging.debug('Checking Maven for JAVA_HOME...')
130+
try:
131+
output = subprocess.check_output(['mvn', '-v'])
132+
if isinstance(output, bytes) and not isinstance(output, str):
133+
#PY3 related
134+
output = output.decode('utf-8')
135+
for part in output.split('\n'):
136+
if part.startswith('Java home:'):
137+
path = part.split(':')[1].strip()
138+
if path.endswith('jre'):
139+
return path[0:-3]
140+
141+
except Exception:
142+
# maven probably isn't installed or not on PATH
143+
logging.debug('Maven not found on PATH. No JAVA_HOME found.')
144+
127145
return None
128146

129147

0 commit comments

Comments
 (0)