Skip to content

Commit

Permalink
chore: JVM error improvement for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
zepfred committed Aug 12, 2024
1 parent a726146 commit 6ae1684
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion python/jpyinterpreter/src/main/python/jvm_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ def init(*args, path: List[str] = None, include_translator_jars: bool = True,
from ai.timefold.jpyinterpreter import InterpreterStartupOptions # noqa
InterpreterStartupOptions.classOutputRootPath = class_output_path

import ai.timefold.jpyinterpreter.CPythonBackedPythonInterpreter as CPythonBackedPythonInterpreter
error = None
try:
import ai.timefold.jpyinterpreter.CPythonBackedPythonInterpreter as CPythonBackedPythonInterpreter
except ImportError as e:
error = str(e)
if error is not None and "due to incorrect Java version" in error:
raise RuntimeError("""Timefold Solver for Python requires JVM (java) version 17 or later. Your JVM version is not supported.
Maybe use sdkman (https://sdkman.io) to install a more modern version of Java.""")
CPythonBackedPythonInterpreter.lookupPythonReferenceIdPythonFunction = GetPythonObjectId()
CPythonBackedPythonInterpreter.lookupPythonReferenceTypePythonFunction = GetPythonObjectType()
CPythonBackedPythonInterpreter.lookupAttributeOnPythonReferencePythonFunction = GetAttributeOnPythonObject()
Expand Down
7 changes: 6 additions & 1 deletion python/python-core/src/main/python/_timefold_java_interop.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def init(*args, path: list[str] = None, include_timefold_jars: bool = True) -> N
"""
from _jpyinterpreter import init


if jpype.isJVMStarted(): # noqa
raise RuntimeError('JVM already started. Maybe call init before timefold.solver.types imports?')
if path is None:
Expand All @@ -86,7 +87,11 @@ def init(*args, path: list[str] = None, include_timefold_jars: bool = True) -> N
if include_timefold_jars:
path = path + extract_timefold_jars()
if len(args) == 0:
args = [jpype.getDefaultJVMPath()]
try:
args = [jpype.getDefaultJVMPath()]
except jpype.JVMNotFoundException:
raise RuntimeError("""Timefold Solver for Python requires JVM (java) version 17 or later. You have none installed.
Maybe use sdkman (https://sdkman.io) to install a modern version of Java.""")
init(*args, path=path, include_translator_jars=False)

from ai.timefold.solver.python.logging import PythonDelegateAppender
Expand Down

0 comments on commit 6ae1684

Please sign in to comment.