From 8f5388e5ae347a94e356880635f8c8add1910d1d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petrus=20Hyv=C3=B6nen?= <petrus.hyvonen@sscspace.com>
Date: Sun, 21 Apr 2024 13:22:27 +0200
Subject: [PATCH] Update of initVM to include possibility of jvm parameters as
 well as additional classpath

---
 orekit_jpype/orekit_jpype.py | 35 +++++++++++++++++++++++++++++------
 orekit_jpype/pyhelpers.py    |  9 ++++++---
 2 files changed, 35 insertions(+), 9 deletions(-)

diff --git a/orekit_jpype/orekit_jpype.py b/orekit_jpype/orekit_jpype.py
index 2b6c19c..ac8c678 100644
--- a/orekit_jpype/orekit_jpype.py
+++ b/orekit_jpype/orekit_jpype.py
@@ -1,20 +1,43 @@
 import jpype
 import jpype.imports  # This enables direct import of java classes
+from typing import List, Union
 #import jpype.beans  # This creates pythonic versions of getters / setters (as in JCC version)
 
-#from jpype.types import *
-
 import os
+
+# Get the  path of the current file, used for finding the jars directory
 dirpath = os.path.dirname(os.path.abspath(__file__))
 
+def initVM(vmargs:Union[str, None]=None, additional_classpaths:Union[List, None]=None):
+    """
+    Initializes the Java Virtual Machine (JVM) for Orekit.
+
+    Args:
+        vmargs (Union[str, None], optional): Additional arguments to pass to the JVM. Defaults to None. 
+             Example for debugging: vmargs='-Xcheck:jni,-verbose:jni,-verbose:class,-XX:+UnlockDiagnosticVMOptions'
+        additional_classpaths (Union[List, None], optional): Additional classpaths to add to the JVM. Defaults to None.
 
-def initVM():
-    jpype.addClassPath(os.path.join(dirpath, 'jars','*'))
+    Raises:
+        FileNotFoundError: If any of the additional classpaths do not exist.
 
-    # Launch the JVM
+    """
+    # Set the classpath
+    if additional_classpaths is not None:
+        for classpath in additional_classpaths:
+            if not os.path.exists(classpath):
+                raise FileNotFoundError(f"Classpath {os.path.abspath(classpath)} does not exist")
+            jpype.addClassPath(os.path.abspath(classpath))
+
+    # Add standard orekit jars to the classpath
     if not jpype.isJVMStarted():
+        jpype.addClassPath(os.path.join(dirpath, 'jars','*'))
+
+        # Start the JVM
         # '-Xcheck:jni','-verbose:jni','-verbose:class'
-        jpype.startJVM( convertStrings=True)
+        if vmargs is not None:
+            jpype.startJVM(*vmargs.split(","), convertStrings=True)
+        else:
+            jpype.startJVM( convertStrings=True)
     else:
         print("JVM already started, resuming on started JVM")
 
diff --git a/orekit_jpype/pyhelpers.py b/orekit_jpype/pyhelpers.py
index ca65e97..611befc 100644
--- a/orekit_jpype/pyhelpers.py
+++ b/orekit_jpype/pyhelpers.py
@@ -22,6 +22,11 @@ from datetime import datetime
 import math
 import os
 from typing import List, Union
+
+import jpype
+import jpype.imports
+from jpype.types import *
+
 from java.io import File
 from org.orekit.data import DataProvidersManager, ZipJarCrawler, DirectoryCrawler, DataContext, LazyLoadedDataContext
 from org.orekit.time import TimeScalesFactory, AbsoluteDate
@@ -36,9 +41,7 @@ try:
 except ImportError:
     import urllib as urlrequest
 
-import jpype
-import jpype.imports
-from jpype.types import *
+
 
 def download_orekit_data_curdir(filename='orekit-data.zip'):
     """
-- 
GitLab