Timefold library needs Java 17

I am trying to get the Timefold library working in a python function (I need to go via python and not java as I need to edit the ontology). It seemed to work ok in live preview, ie it is showing me the ontology edits that I was expecting. However, when I tag and release the function and ping it via a button in Workshop, I get this message. Any fixes or is it best to find another algorithm (pity)?

InvalidJVMVersionError: Timefold Solver for Python requires JVM (java) version 17 or later. You have none installed. Maybe use sdkman (https://sdkman.io) to install a more modern version of Java..
Error Parameters: {
  "module": "python_functions.my_function"
}
Traceback (most recent call last):
  File "/app/var/data/conda-env/lib/python3.12/site-packages/_jpyinterpreter/jvm_setup.py", line 304, in get_default_jvm_path
    return jvm_getter()
           ^^^^^^^^^^^^
  File "/app/var/data/conda-env/lib/python3.12/site-packages/jpype/_jvmfinder.py", line 70, in getDefaultJVMPath
    return finder.get_jvm_path()
           ^^^^^^^^^^^^^^^^^^^^^
  File "/app/var/data/conda-env/lib/python3.12/site-packages/jpype/_jvmfinder.py", line 204, in get_jvm_path
    raise JVMNotFoundException("No JVM shared library file ({0}) "
jpype._jvmfinder.JVMNotFoundException: No JVM shared library file (libjvm.so) found. Try setting up the JAVA_HOME environment variable properly.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/var/data/conda-env/lib/python3.12/site-packages/python_functions/my_function.py", line 50, in <module>
    @planning_entity
     ^^^^^^^^^^^^^^^
  File "/app/var/data/conda-env/lib/python3.12/site-packages/timefold/solver/domain/_annotations.py", line 773, in planning_entity
    ensure_init()
  File "/app/var/data/conda-env/lib/python3.12/site-packages/timefold/solver/_timefold_java_interop.py", line 191, in ensure_init
    init()
  File "/app/var/data/conda-env/lib/python3.12/site-packages/timefold/solver/_timefold_java_interop.py", line 90, in init
    args = [get_default_jvm_path()]
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/app/var/data/conda-env/lib/python3.12/site-packages/_jpyinterpreter/jvm_setup.py", line 306, in get_default_jvm_path
    raise InvalidJVMVersionError(
Timefold Solver for Python requires JVM (java) version 17 or later. You have none installed. Maybe use sdkman (https://sdkman.io) to install a more modern version of Java.

I found the solution. Live preview has the right java stuff installed but when you tag the function, this all falls away. So you need to install openjdk library via conda, then find its path (mine is shown below, yours should be the same). You also need to put all the timefold code in a wrapper function and call that in the main function, i.e.:

def init_timefold():
    os.environ["JAVA_HOME"] = "/app/var/data/conda-env/lib/jvm"
    os.environ["LD_LIBRARY_PATH"] = "/app/var/data/conda-env/lib/jvm/lib/server"
    # all your class code
    return Timeslot, Room, Surgery, TheatreSchedule, define_constraints, SolverFactory, SolverConfig, ScoreDirectorFactoryConfig, TerminationConfig, Duration
    
@function(edits=[Theatre])
def optimise_theatre(optimal_os: TheatreObjectSet) -> list[OntologyEdit]:
    ontology_edits = FoundryClient().ontology.edits()
    Timeslot, Room, Surgery, TheatreSchedule, define_constraints, SolverFactory, SolverConfig, ScoreDirectorFactoryConfig, TerminationConfig, Duration = **init_timefold()**
etc

If you want to test via live preview, just comment out the 2 path lines. Works great!