Skip to content

Commit

Permalink
Remove the need for late load... Everyone is equal!
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Nov 28, 2024
1 parent a0b2b3e commit 94f21a1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
30 changes: 9 additions & 21 deletions jpype/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,27 +293,32 @@ def startJVM(
import shutil
tmp = tempfile.gettempdir()
if not tmp.isascii():
raise ValueError("Unable to create ascii temp directory. Clear TEMPDIR, TEMP, and TMP environment variables")
raise ValueError("Unable to find ascii temp directory. Clear TEMPDIR, TEMP, and TMP environment variables")
remove = os.path.join(tmp, "org.jpype.jar")
shutil.copyfile(support_lib, remove)
support_lib = remove

java_class_path = _expandClassPath(classpath)
java_class_path.append(support_lib)
java_class_path = _classpath._SEP.join(java_class_path)

# ok, setup the jpype system classloader and add to the path after startup
# this guarentees all classes have the same permissions as they did in the past
extra_jvm_args += [
'-Djava.system.class.loader=org.jpype.classloader.DynamicClassLoader',
'-Djava.class.path=%s'%support_lib,
'-Djpype.class.path=%s'%java_class_path,
'-Xshare:off'
]
late_load = True
else:
# no problems
extra_jvm_args += ['-Djava.class.path=%s'%java_class_path ]
late_load = False

if agent:
extra_jvm_args += ['-javaagent:' + support_lib]

print("extra", extra_jvm_args)

try:
import locale
# Gather a list of locale settings that Java may override (excluding LC_ALL)
Expand Down Expand Up @@ -341,24 +346,7 @@ def startJVM(
raise RuntimeError(f"{jvmpath} is older than required Java version{version}") from ex
raise

"""Prior versions of JPype used the jvmargs to setup the class paths via
JNI (Java Native Interface) option strings:
i.e -Djava.class.path=...
See: https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/invocation.html
Unfortunately, only ascii paths work because url encoding is not handled correctly
see: https://bugs.openjdk.org/browse/JDK-8079633?jql=text%20~%20%22ParseUtil%22
To resolve this issue, we add the classpath after initialization since Java has
had the utilities to correctly encode it since 1.0
"""
if late_load and classpath:
# now we can add to the system classpath
cl = _jpype.JClass("java.lang.ClassLoader").getSystemClassLoader()
from pathlib import Path
for cp in _expandClassPath(classpath):
print("Late load", cp)
cl.addFile(Path(cp))
# Clean up
if remove is not None:
os.remove(remove)

Expand Down
5 changes: 2 additions & 3 deletions test/jpypetest/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ def setUp(self):
args.append(
"-javaagent:lib/org.jacoco.agent-0.8.5-runtime.jar=destfile=build/coverage/jacoco.exec,includes=org.jpype.*")
warnings.warn("using JaCoCo")
import pathlib
jpype.addClassPath(pathlib.Path("lib/*").absolute())
jpype.addClassPath(pathlib.Path("test/jar/*").absolute())
jpype.addClassPath(path.join(root, "../lib/*"))
jpype.addClassPath(path.join(root, "jar/*"))
classpath_arg %= jpype.getClassPath()
args.append(classpath_arg)
_jpype.enableStacktraces(True)
Expand Down

0 comments on commit 94f21a1

Please sign in to comment.