Using 3rd party java libraries

Asked by Brian Cerny

Hi,

What is the best way to use 3rd party Java libraries with the SikuliX IDE?

Since this is Jython, I tried using load(...), which does add the jar to the sys.path variable, but the import statement fails.

I'm trying to use JNA (https://github.com/java-native-access/jna) so I can look through the window titles and find the one and then bring it to the front.

load('jna-4.3.0.jar')
load('jna-platform-4.3.0.jar')

from com.sun.jna.platform.win32 import *

[error] ImportError ( No module named jna )

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Brian Cerny
Solved:
Last query:
Last reply:
Revision history for this message
Brian Cerny (briancerny) said :
#1

Also tried setting the SIKULIX_HOME environment variable and added the jars to the lib subfolder. This subfolder is on sys.path too but it still doesn't find the jars.

Using Jython 2.7

Revision history for this message
Brian Cerny (briancerny) said :
#2

According to the Jython Docs, it should be easy to do. However, following the code sample produces less than satisfactory results.

http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#module-search-path-and-loading

import sys

sys.path.append('c:/users/brian/download/jna-platform-4.3.0.jar')

import com.sun.jna

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named jna

Revision history for this message
RaiMan (raimund-hocke) said :
#3

the spec
com.sun.jna.platform.win32
is not correct, it is
com.sun.jna.win32

tested with Jython 2.7 on commandline:

being in the folder that contains the jna-4.3.0.jar

- take care, that sikulixapi.jar is in the environment list CLASSPATH (Java classpath)
- run Jython interactive prompt
- issue: import org.sikuli.script.SikulixForJython
- issue: from sikuli import *
- issue: load("jna-4.3.0.jar")

then:
from com.sun.jna.win32 import *

works for me.

I did not test any JNA feature yet ;-)

Revision history for this message
Brian Cerny (briancerny) said :
#4

Thanks for the reply.

After restarting my dev machine and running the command prompt as an administrator, I was able to get it working from the command line with both Jython 2.7 and 2.5. It does not work as a non-administrator on Windows though (verified).

com.sun.jna.win32 is in jna-4.3.0.jar and
com.sun.jna.platform.win32 is in jna-platform-4.3.0.jar

Output:
import sys

sys.path.append('C:\Users\bcerny.HRA\Downloads\jna-4.3.0.jar')
import com.sun.jna

sys.path.append('C:\Users\bcerny.HRA\Downloads\jna-platform-4.3.0.jar')
import com.sun.jna.platform.win32

However, I'm still having issues doing the same thing in the SikuliX IDE even when running it as an administrator.

Tried both the load method and the sys.path.append approach, and neither worked while running the SikuliX IDE as an administrator.

Any ideas?

Revision history for this message
Brian Cerny (briancerny) said :
#5

Increased the log level of Sikulix to 3 and saw this in the log:

[debug] JythonScriptRunner: runPython: running script from IDE: C:/Users/Brian/Desktop/fw.sikuli
[debug] JythonSupport: load: to be loaded: C:\users\Brian\Downloads\jna-4.3.0.jar
[debug] RunTimeIDE: addToClasspath: file:/C:/Users/Brian/Downloads/jna-4.3.0.jar
[debug] JythonSupport: load: to be loaded: C:\users\Brian\Downloads\jna-platform-4.3.0.jar
[debug] RunTimeIDE: addToClasspath: file:/C:/Users/Brian/Downloads/jna-platform-4.3.0.jar

[error] script [ fw ] stopped with error in line 11
[error] ImportError ( No module named jna )

Line 11: import com.sun.jna

Revision history for this message
RaiMan (raimund-hocke) said :
#6

ok, I will check on Windows.

... but have you tried this already in the IDE?

import sys
sys.path.append('C:\Users\bcerny.HRA\Downloads\jna-4.3.0.jar')
import com.sun.jna
sys.path.append('C:\Users\bcerny.HRA\Downloads\jna-platform-4.3.0.jar')
import com.sun.jna.platform.win32

Revision history for this message
Brian Cerny (briancerny) said :
#7

Yes, I tried both in the IDE and as a script. In both cases it failed.

I created a new Java project where I'm using the Jython PythonInterpreter by itself and executing the statements. It fails in the interpreter too, so I'm thinking it might be related to the interpreter or how it's setup.

Revision history for this message
Brian Cerny (briancerny) said :
#8

OK, it seems something is slightly different between the Jython REPL and the Jython PhytonInterpreter class which is used internally in SikuliX.

In the REPL, i can do:
  from com.sun.jna.platform.win32 import *
but this does not work with the interpreter.

In the interpreter, I must do:
  from com.sun.jna.platform.win32 import Win32
and import each one separately

Maybe something to do with Java package scanning as detailed here:
http://www.jython.org/jythonbook/en/1.0/ModulesPackages.html#java-package-scanning

Not a SikuliX issue so I'm marking as solved since there is a reasonable workaround.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

interesting. thanks.

Revision history for this message
RaiMan (raimund-hocke) said :
#10

I would appreciate to get your "window title search" solution, at least the part, that uses the JNA/win32 features.

I am currently using the package Bridj (https://github.com/nativelibs4java/BridJ) for win32 path handling, and plan to migrate the C/C++ code beneath the App class by a Java/JNA implementation.

So your example would be helpful.