Skip to content

Commit 3267891

Browse files
committed
Fix add_library for windows. Fix #308. Fix #227.
Adding paths to java.library.path in order to load a .dll wasn't working on windows. I'm not sure how this is working on any platform, but everything seems to be working fine. Split recursivelyAddToClasspath into two different methods, one to add .jars to the classpath, and one to load .dlls using the absolute file path. These changes are only for the windows platform, all other platforms function the old way.
1 parent 016ddc5 commit 3267891

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

runtime/src/jycessing/LibraryImporter.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.python.google.common.base.Joiner;
3030
import org.python.util.InteractiveConsole;
3131

32+
import processing.app.Platform;
3233
import processing.core.PApplet;
3334

3435
/**
@@ -128,19 +129,63 @@ protected void addLibrary(final String libName) {
128129
if (libDir == null) {
129130
interp.exec("raise Exception('This sketch requires the \"" + libName + "\" library.')");
130131
}
132+
131133
final File contentsDir = new File(libDir, "library");
132134
if (!contentsDir.exists()) {
133135
interp.exec("raise Exception('The library " + libName + " is malformed and won't import.')");
134136
}
135137
final File mainJar = new File(contentsDir, libName + ".jar");
136138

137-
recursivelyAddToClasspath(contentsDir);
139+
log("mainJar: " + mainJar);
140+
log("Adding dir: " + contentsDir);
141+
if (Platform.isWindows()) {
142+
final File nativeDir = new File(libDir, "library/windows" + Platform.getVariant());
143+
recursivelyAddJarsToClasspath(contentsDir);
144+
recursivelyLoadNativeLibraryies(nativeDir);
145+
} else {
146+
recursivelyAddToClasspath(contentsDir);
147+
}
138148

139149
if (mainJar.exists()) {
150+
log("adding mainJar");
140151
importPublicClassesFromJar(mainJar);
141152
}
142153
}
143154

155+
private void recursivelyAddJarsToClasspath(final File contentsDir) {
156+
final List<File> resources = findResources(contentsDir);
157+
final PySystemState sys = Py.getSystemState();
158+
for (final File resource : resources) {
159+
final String name = resource.getName();
160+
if (name.endsWith(".jar") || name.endsWith(".zip")) {
161+
// Contains stuff we want
162+
addJarToClassLoader(resource.getAbsoluteFile());
163+
164+
log("Appending " + resource.getAbsolutePath() + " to sys.path.");
165+
sys.path.append(Py.newStringUTF8(resource.getAbsolutePath()));
166+
} else if (resource.isDirectory()) {
167+
recursivelyAddToClasspath(resource);
168+
}
169+
}
170+
}
171+
172+
private void recursivelyLoadNativeLibraryies(final File contentsDir) {
173+
final List<File> resources = findResources(contentsDir);
174+
final PySystemState sys = Py.getSystemState();
175+
for (final File resource : resources) {
176+
final String name = resource.getName();
177+
if (name.matches("^.*\\.(so|dll|dylib|jnilib)$")) {
178+
// Add *containing directory* to native search path
179+
log("addDirectoryToNativeSearchPath: " + resource.getAbsoluteFile().getParentFile().getAbsolutePath());
180+
addDirectoryToNativeSearchPath(resource.getAbsoluteFile().getParentFile());
181+
log("Loading library: " + resource.getAbsoluteFile().getName());
182+
System.load(resource.getAbsolutePath());
183+
} else if (resource.isDirectory()) {
184+
recursivelyAddToClasspath(resource);
185+
}
186+
}
187+
}
188+
144189
private void recursivelyAddToClasspath(final File contentsDir) {
145190
final List<File> resources = findResources(contentsDir);
146191
final PySystemState sys = Py.getSystemState();
@@ -154,6 +199,7 @@ private void recursivelyAddToClasspath(final File contentsDir) {
154199
sys.path.append(Py.newStringUTF8(resource.getAbsolutePath()));
155200
} else if (name.matches("^.*\\.(so|dll|dylib|jnilib)$")) {
156201
// Add *containing directory* to native search path
202+
log("addDirectoryToNativeSearchPath: " + resource.getAbsoluteFile().getParentFile().getName());
157203
addDirectoryToNativeSearchPath(resource.getAbsoluteFile().getParentFile());
158204
} else if (resource.isDirectory()) {
159205
recursivelyAddToClasspath(resource);

testmode.bat

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
set VERBOSE_PYTHON_MODE=true
2+
3+
set PROCESSINGPY=%CD%
4+
set PROCESSING=..\processing
5+
6+
for /f "tokens=1,2*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" 2^>nul') do set MY_DOCS_ROOT=%%C
7+
8+
set MODES=%MY_DOCS_ROOT%\Processing\modes
9+
set RUNPROCESSINGDIR=%PROCESSING%\build\windows\work
10+
11+
cd /d %PROCESSINGPY%
12+
ant mode.zip
13+
14+
cd /d %MODES%
15+
del /s /f /q PythonMode
16+
for /f %%f in ('dir /ad /b PythonMode') do rd /s /q PythonMode\%%f
17+
18+
cd /d %PROCESSINGPY%\work
19+
powershell Expand-Archive PythonMode.zip -DestinationPath %MODES%
20+
21+
cd /d %PROCESSINGPY%
22+
23+
cd /d %RUNPROCESSINGDIR%
24+
.\java\bin\java -cp lib\pde.jar;core\library\core.jar;lib\jna.jar;lib\jna-platform.jar;lib\antlr.jar;lib\ant.jar;lib\ant-launcher.jar processing.app.Base
25+
26+
cd %PROCESSINGPY%

0 commit comments

Comments
 (0)