Skip to content

Commit

Permalink
Working on loading into module
Browse files Browse the repository at this point in the history
  • Loading branch information
Thrameos committed Dec 2, 2024
1 parent b932bad commit 773ec40
Show file tree
Hide file tree
Showing 6 changed files with 1,847 additions and 27 deletions.
62 changes: 39 additions & 23 deletions jpype/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ def _expandClassPath(
Return a classpath which represents the given tuple of classpath specifications
"""
out: list[str] = []
if classpath is None:
return out
if isinstance(classpath, (str, os.PathLike)):
classpath = (classpath,)
try:
Expand Down Expand Up @@ -195,6 +197,7 @@ def startJVM(
jvmpath: typing.Optional[_PathOrStr] = None,
classpath: typing.Union[typing.Sequence[_PathOrStr], _PathOrStr, None] = None,
modulepath: typing.Union[typing.Sequence[_PathOrStr], _PathOrStr, None] = None,
modules: typing.Union[typing.Sequence[str], str, None] = None,
ignoreUnrecognized: bool = False,
convertStrings: bool = False,
interrupt: bool = not interactive(),
Expand Down Expand Up @@ -270,6 +273,24 @@ def startJVM(
# Handle strings and list of strings.
extra_jvm_args: list[str] = []

# Get the support library
support_lib = os.path.join(os.path.dirname(os.path.dirname(__file__)), "org.jpype.jar")
if not os.path.exists(support_lib):
raise RuntimeError("Unable to find org.jpype.jar support library at " + support_lib)

# If we are not installed on an ascii path then we will need to copy the jar to a new location
if not support_lib.isascii():
import tempfile
import shutil
fd, path = tempfile.mkstemp(dir = _findTemp())
if not path.isascii():
raise ValueError("Unable to find ascii temp directory.")
shutil.copyfile(support_lib, path)
support_lib = path
tmp = path
os.close(fd)
# Don't remove

# Classpath handling
old_classpath = _getOption(jvm_args, "-Djava.class.path", _classpath._SEP)
if old_classpath:
Expand All @@ -282,7 +303,6 @@ def startJVM(
# Not specified at all, use the default classpath.
classpath = _classpath.getClassPath()

# Code for 1.6 release when we add module support
# Modulepath handling
old_modulepath = _getOption(jvm_args, "--module-path", _classpath._SEP)
if old_modulepath:
Expand All @@ -291,19 +311,22 @@ def startJVM(
# Cannot apply both styles, conflict
raise TypeError('modulepath specified twice')
modulepath = old_modulepath
if modulepath is not None:
mp = _classpath._SEP.join(_expandClassPath(modulepath))
extra_jvm_args += ['--module-path=%s'%mp ]

# Get the support library
support_lib = os.path.join(os.path.dirname(os.path.dirname(__file__)), "org.jpype.jar")
if not os.path.exists(support_lib):
raise RuntimeError("Unable to find org.jpype.jar support library at " + support_lib)
# Modules
old_modules = _getOption(jvm_args, "--add-modules", ',')
if old_modules:
# Old style, specified in the arguments
if modules is not None:
# Cannot apply both styles, conflict
raise TypeError('modules specified twice')
modules = old_modules
if modules is None:
modules = []
modules.append("org.jpype")

system_class_loader = _getOption(jvm_args, "-Djava.system.class.loader", keep=True)

java_class_path = _expandClassPath(classpath)
java_class_path.append(support_lib)
java_class_path = list(filter(len, java_class_path))
classpath = _classpath._SEP.join(java_class_path)
tmp = None
Expand All @@ -314,19 +337,6 @@ def startJVM(
# https://bugs.openjdk.org/browse/JDK-8079633?jql=text%20~%20%22ParseUtil%22
raise ValueError("system classloader cannot be specified with non ascii characters in the classpath")

# If we are not installed on an ascii path then we will need to copy the jar to a new location
if not support_lib.isascii():
import tempfile
import shutil
fd, path = tempfile.mkstemp(dir = _findTemp())
if not path.isascii():
raise ValueError("Unable to find ascii temp directory.")
shutil.copyfile(support_lib, path)
support_lib = path
tmp = path
os.close(fd)
# Don't remove

# 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
from urllib.parse import quote
Expand All @@ -338,7 +348,13 @@ def startJVM(
]
else:
# no problems
extra_jvm_args += ['-Djava.class.path=%s'%classpath ]
if classpath:
extra_jvm_args += ['-Djava.class.path=%s'%classpath ]

mp =_expandClassPath(modulepath)
mp.append(support_lib)
extra_jvm_args += ['--module-path=%s'%_classpath._SEP.join(mp) ]
extra_jvm_args += ['--add-modules=%s'%(",".join(modules))]

try:
import locale
Expand Down
1 change: 1 addition & 0 deletions project/jpype_java/nb-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ Any value defined here will override the pom.xml file value but is only applicab
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.methodDeclBracePlacement>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantWhileBraces>LEAVE_ALONE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.redundantWhileBraces>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>NEW_LINE</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.classDeclBracePlacement>
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
</properties>
</project-shared-configuration>
18 changes: 18 additions & 0 deletions project/jpype_java/src/main/java/org/jpype/JPypeMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package org.jpype;

/**
*
* @author nelson85
*/
public class JPypeMain
{
public static void main(String[] args)
{
JPypeMain main = new JPypeMain();
System.out.println(main.getClass().getModule());
}
}
Loading

0 comments on commit 773ec40

Please sign in to comment.