66import logging
77import os
88import re
9- import shutil
109import subprocess
1110import sys
1211from functools import lru_cache
1716import jpype .config
1817from jgo import jgo
1918
20- from scyjava ._cjdk_fetch import cjdk_fetch_java , cjdk_fetch_maven
19+ from scyjava ._cjdk_fetch import ensure_jvm_available
2120import scyjava .config
2221from scyjava .config import Mode , mode
2322
@@ -120,10 +119,12 @@ def start_jvm(options=None, *, fetch_java: bool | None = None) -> None:
120119 List of options to pass to the JVM.
121120 For example: ['-Dfoo=bar', '-XX:+UnlockExperimentalVMOptions']
122121 :param fetch_java:
123- Whether to automatically fetch a JRE (and/or maven) using
122+ Whether to automatically fetch a JRE (and maven) using
124123 [`cjdk`](https://github.com/cachedjdk/cjdk) if java and maven executables are
125- not found. Requires `cjdk` to be installed. See README for details.
124+ not found. Requires `cjdk` to be installed, either manually, or via the
125+ `scyjava[cjdk]` extra.
126126 - If `None` (default), then fetching will only occur if `cjdk` is available.
127+ (A log info will be issued if `cjdk` is not available.)
127128 - If `True`, an exception will be raised if `cjdk` is not available.
128129 - If `False`, no attempt to import `cjdk` is be made.
129130 """
@@ -141,14 +142,11 @@ def start_jvm(options=None, *, fetch_java: bool | None = None) -> None:
141142 # use the logger to notify user that endpoints are being added
142143 _logger .debug ("Adding jars from endpoints {0}" .format (endpoints ))
143144
144- if fetch_java is not False and not is_jvm_available () :
145- cjdk_fetch_java (raise_on_error = fetch_java is True )
145+ if fetch_java is not False :
146+ ensure_jvm_available (raise_on_error = fetch_java is True )
146147
147148 # get endpoints and add to JPype class path
148149 if len (endpoints ) > 0 :
149- if not shutil .which ("mvn" ) and fetch_java is not False :
150- cjdk_fetch_maven (raise_on_error = fetch_java is True )
151-
152150 endpoints = endpoints [:1 ] + sorted (endpoints [1 :])
153151 _logger .debug ("Using endpoints %s" , endpoints )
154152 _ , workspace = jgo .resolve_dependencies (
@@ -355,28 +353,6 @@ def is_jvm_headless() -> bool:
355353 return bool (GraphicsEnvironment .isHeadless ())
356354
357355
358- def is_jvm_available () -> bool :
359- """
360- Return True if the JVM is available, suppressing stderr on macos.
361- """
362- from unittest .mock import patch
363-
364- subprocess_check_output = subprocess .check_output
365-
366- def _silent_check_output (* args , ** kwargs ):
367- # also suppress stderr on calls to subprocess.check_output
368- kwargs .setdefault ("stderr" , subprocess .DEVNULL )
369- return subprocess_check_output (* args , ** kwargs )
370-
371- try :
372- with patch .object (subprocess , "check_output" , new = _silent_check_output ):
373- jpype .getDefaultJVMPath ()
374- # on Darwin, may raise a CalledProcessError when invoking `/user/libexec/java_home`
375- except (jpype .JVMNotFoundException , subprocess .CalledProcessError ):
376- return False
377- return True
378-
379-
380356def is_awt_initialized () -> bool :
381357 """
382358 Return true iff the AWT subsystem has been initialized.
0 commit comments