addHotkey cannot coerce 3rd argument to HotkeyListener

Asked by Peter Zejda on 2020-04-08

I am using this version of the IDE: 2.0.4-2020-03-14_08:01/Windows10.0/Java8(64)1.8.0_241-b07

When I use the following program in the Sikuli IDE, it works as expected, and I can break out of the program by typing Ctrl-Shft-F:

running = True
def quitApp(event):
    print "handler quit ctrl-shft-f"
    global running
    running = False

Env.addHotkey('f', KeyModifier.CTRL + KeyModifier.SHIFT, quitApp)

while running:
    print "hello"
    sleep(1)

So far so good. Then I try to run the exact same program from Jython command line (with Sikulixapi-2.0.4.jar on the CLASSPATH), with an additional line to import the Sikuli scripts:

from org.sikuli.script import *

running = True
def quitApp(event):
    print "handler quit ctrl-shft-f"
    global running
    running = False

Env.addHotkey('f', KeyModifier.CTRL + KeyModifier.SHIFT, quitApp)

while running:
    print "hello"
    sleep(1)

However, when I run this, I get the following error:

Traceback (most recent call last):
  File "temp.py", line 10, in <module>
    Env.addHotkey('f', KeyModifier.CTRL + KeyModifier.SHIFT, quitApp)
TypeError: addHotkey(): 3rd arg can't be coerced to org.sikuli.basics.HotkeyListener

Any idea what might be causing the problem?

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2020-04-09
Last query:
2020-04-09
Last reply:
2020-04-09
RaiMan (raimund-hocke) said : #1

from org.sikuli.script import *

should not be used and is not necessary in any case.

Only in sub-scripts (Python scripts called from a main script) you need: (see docs)
from sikuli import *

Peter Zejda (ptkeen) said : #2

Hi RaiMan, thanks for the quick response!

If I remove the "from org.sikuli.script.import *" line, then I get this error:

Traceback (most recent call last):
  File "C:/Users/Peter Zejda/Google Drive/home/programming/fun/skipads.sikuli/temp.py", line 7, in <module>
    Env.addHotkey('f', KeyModifier.CTRL + KeyModifier.SHIFT, quitApp)
NameError: name 'Env' is not defined

Process finished with exit code -1

If I replace it with the line "from sikuli import *", then I get this error:

Traceback (most recent call last):
  File "C:/Users/Peter Zejda/Google Drive/home/programming/fun/skipads.sikuli/temp.py", line 1, in <module>
    from sikuli import *
ImportError: No module named sikuli

Process finished with exit code -1

I read a lot of the documents, and also the did some fair amount of Googling, and also tried many variations, and that's how I eventually ended up with the "from org.sikuli.script.import *" being the closest thing that worked (at least it then recognized Env and KeyModifier). I also tried using the following lines that were suggest for a Python console in Pycharm:

import org.sikuli.script.SikulixForJython
from sikuli import *

While this seems to work well within the Console (plus an additional couple of lines before it to print out the Python version and add the working directory to the path), when run in the stand-alone script it gives:

Traceback (most recent call last):
  File "C:/Users/Peter Zejda/Google Drive/home/programming/fun/skipads.sikuli/temp.py", line 2, in <module>
    from sikuli import *
  File "C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\sikuli\__init__.py", line 3, in <module>
    from Sikuli import *
  File "C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\sikuli\Sikuli.py", line 88, in <module>
    import org.sikuli.script.TextRecognizer as JTextOCR
java.lang.NoClassDefFoundError: net/sourceforge/tess4j/TesseractException
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at org.python.core.Py.loadAndInitClass(Py.java:1107)
 at org.python.core.Py.findClassInternal(Py.java:1042)
 at org.python.core.Py.findClassEx(Py.java:1093)
 at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:142)
 at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33)
 at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:130)
 at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:134)
 at org.python.core.PyObject.__findattr__(PyObject.java:965)
 at org.python.core.PyObject.impAttr(PyObject.java:1103)
 at org.python.core.imp.import_next(imp.java:840)
 at org.python.core.imp.import_logic(imp.java:905)
 at org.python.core.imp.import_module_level(imp.java:970)
 at org.python.core.imp.importName(imp.java:1057)
 at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
 at org.python.core.PyObject.__call__(PyObject.java:450)
 at org.python.core.__builtin__.__import__(__builtin__.java:1232)
 at org.python.core.imp.importOneAs(imp.java:1093)
 at sikuli.Sikuli$py.f$0(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/Sikuli.py:669)
 at sikuli.Sikuli$py.call_function(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/Sikuli.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:171)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.imp.createFromCode(imp.java:436)
 at org.python.core.imp.createFromSource(imp.java:396)
 at org.python.core.imp.loadFromSource(imp.java:664)
 at org.python.core.imp.find_module(imp.java:551)
 at org.python.core.PyModule.impAttr(PyModule.java:111)
 at org.python.core.imp.import_next(imp.java:840)
 at org.python.core.imp.import_module_level(imp.java:957)
 at org.python.core.imp.importName(imp.java:1057)
 at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
 at org.python.core.PyObject.__call__(PyObject.java:450)
 at org.python.core.__builtin__.__import__(__builtin__.java:1232)
 at org.python.core.imp.importAll(imp.java:1176)
 at sikuli$py.f$0(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/__init__.py:3)
 at sikuli$py.call_function(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/__init__.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:171)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.imp.createFromCode(imp.java:436)
 at org.python.core.imp.createFromSource(imp.java:396)
 at org.python.core.imp.loadFromSource(imp.java:664)
 at org.python.core.imp.find_module(imp.java:551)
 at org.python.core.imp.import_next(imp.java:838)
 at org.python.core.imp.import_module_level(imp.java:957)
 at org.python.core.imp.importName(imp.java:1057)
 at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
 at org.python.core.PyObject.__call__(PyObject.java:450)
 at org.python.core.__builtin__.__import__(__builtin__.java:1232)
 at org.python.core.imp.importAll(imp.java:1176)
 at org.python.pycode._pyx0.f$0(C:/Users/Peter Zejda/Google Drive/home/programming/fun/skipads.sikuli/temp.py:12)
 at org.python.pycode._pyx0.call_function(C:/Users/Peter Zejda/Google Drive/home/programming/fun/skipads.sikuli/temp.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:171)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.Py.runCode(Py.java:1614)
 at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:296)
 at org.python.util.jython.run(jython.java:362)
 at org.python.util.jython.main(jython.java:142)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.tess4j.TesseractException
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 ... 58 more
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: net/sourceforge/tess4j/TesseractException

Process finished with exit code -1

I also have the following environment variables set up, though I'm not sure they are all necessary:

CLASSPATH=C:\non-installed\SikulixIDE204\sikulixapi-2.0.4.jar
JYTHONPATH=C:\non-installed\jython2.7.1\bin
JYTHON_HOME=C:\non-installed\jython2.7.1

Best RaiMan (raimund-hocke) said : #3

tested on my Windows 10 - works.

I have a Jython 2.7.1 installed at c:\jython2.7.1

I use the following command script to run Jython:

set jython=c:\jython2.7.1\bin\jython.exe
set CLASSPATH=C:\SXRun\_Latest\2_0_4\sikulixide-2.0.4.jar
%jython% %*

Using the script without parameters opens the interactive Jython prompt.

Running this script (<some-folder>\test.py):

import org.sikuli.script.SikulixForJython
from sikuli import *
for e in sys.path: print e

runs as expected without any errors and prints:

C:\Users\rmhde\AppData\Roaming\Sikulix\Lib\site-packages
C:\Users\rmhde\AppData\Roaming\Sikulix\Lib
C:\Users\rmhde
C:\jython2.7.1\Lib
__classpath__
__pyclasspath__/
C:\jython2.7.1\Lib\site-packages

The first import takes care, that the folder <app-data>\Lib is on sys.path.
The second import initializes the API.

Peter Zejda (ptkeen) said : #4

Thanks RaiMan, that solved my question.

Peter Zejda (ptkeen) said : #5

Thanks, RaiMan. I implemented the simple script just as you provided, and also set the same environment variables. It seems that the culprit was that I was using:

CLASSPATH=C:\non-installed\SikulixIDE204\sikulixapi-2.0.4.jar

That is, I was using the API jar file instead of the IDE jar file. I think in reading the various documentation, somehow I had the impression that I need that library when using Sikuli outside of the Sikuli IDE.

Anyway, again, thanks for the pointer.

RaiMan (raimund-hocke) said : #6

Sorry for the mistake.

When running Jython from commandline directly, one should not use the IDE jar, because it already contains all the stuff needed to run Jython - might collide on sys.path.

my final test command script hence was:

set jython=c:\jython2.7.1\bin\jython.exe
set CLASSPATH=C:\SXRun\_Latest\2_0_4\sikulixapi-2.0.4.jar
%jython% %*

this worked also and should be used.

Peter Zejda (ptkeen) said : #7

Hi RaiMan,

Well, it was a happy accident. For me, the IDE jar file works correct, but if I set the CLASSPATH to the API jar file, then it fails. So I guess I can just still to the ID version of the jar file (as a reasonable workaround for myself).

If interested, here is the output with the CLASSPATH being set back and forth, and the corresponding results.

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>jython temp.py
C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\site-packages
C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib
C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli
C:\non-installed\jython2.7.1\bin
C:\non-installed\jython2.7.1\Lib
__classpath__
__pyclasspath__/
C:\non-installed\jython2.7.1\Lib\site-packages

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>echo %CLASSPATH%
C:\non-installed\SikulixIDE204\sikulixide-2.0.4.jar

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>set CLASSPATH=C:\non-installed\SikulixIDE204\api\sikulixapi-2.0.4.jar

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>jython temp.py
Traceback (most recent call last):
  File "temp.py", line 2, in <module>
    from sikuli import *
  File "C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\sikuli\__init__.py", line 3, in <module>
    from Sikuli import *
  File "C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\sikuli\Sikuli.py", line 88, in <module>
    import org.sikuli.script.TextRecognizer as JTextOCR
java.lang.NoClassDefFoundError: net/sourceforge/tess4j/ITesseract
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.python.core.Py.loadAndInitClass(Py.java:1107)
        at org.python.core.Py.findClassInternal(Py.java:1042)
        at org.python.core.Py.findClassEx(Py.java:1093)
        at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:142)
        at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33)
        at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:130)
        at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:134)
        at org.python.core.PyObject.__findattr__(PyObject.java:965)
        at org.python.core.PyObject.impAttr(PyObject.java:1103)
        at org.python.core.imp.import_next(imp.java:840)
        at org.python.core.imp.import_logic(imp.java:905)
        at org.python.core.imp.import_module_level(imp.java:970)
        at org.python.core.imp.importName(imp.java:1057)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:450)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importOneAs(imp.java:1093)
        at sikuli.Sikuli$py.f$0(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/Sikuli.py:669)
        at sikuli.Sikuli$py.call_function(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/Sikuli.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:171)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.imp.createFromCode(imp.java:436)
        at org.python.core.imp.createFromSource(imp.java:396)
        at org.python.core.imp.loadFromSource(imp.java:664)
        at org.python.core.imp.find_module(imp.java:551)
        at org.python.core.PyModule.impAttr(PyModule.java:111)
        at org.python.core.imp.import_next(imp.java:840)
        at org.python.core.imp.import_module_level(imp.java:957)
        at org.python.core.imp.importName(imp.java:1057)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:450)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importAll(imp.java:1176)
        at sikuli$py.f$0(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/__init__.py:3)
        at sikuli$py.call_function(C:/Users/Peter Zejda/AppData/Roaming/Sikulix/Lib/sikuli/__init__.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:171)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.imp.createFromCode(imp.java:436)
        at org.python.core.imp.createFromSource(imp.java:396)
        at org.python.core.imp.loadFromSource(imp.java:664)
        at org.python.core.imp.find_module(imp.java:551)
        at org.python.core.imp.import_next(imp.java:838)
        at org.python.core.imp.import_module_level(imp.java:957)
        at org.python.core.imp.importName(imp.java:1057)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
        at org.python.core.PyObject.__call__(PyObject.java:450)
        at org.python.core.__builtin__.__import__(__builtin__.java:1232)
        at org.python.core.imp.importAll(imp.java:1176)
        at org.python.pycode._pyx0.f$0(temp.py:3)
        at org.python.pycode._pyx0.call_function(temp.py)
        at org.python.core.PyTableCode.call(PyTableCode.java:171)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1614)
        at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:296)
        at org.python.util.jython.run(jython.java:362)
        at org.python.util.jython.main(jython.java:142)
Caused by: java.lang.ClassNotFoundException: net.sourceforge.tess4j.ITesseract
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 58 more
java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: net/sourceforge/tess4j/ITesseract

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>set CLASSPATH=C:\non-installed\SikulixIDE204\sikulixide-2.0.4.jar

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>jython temp.py
C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib\site-packages
C:\Users\Peter Zejda\AppData\Roaming\Sikulix\Lib
C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli
C:\non-installed\jython2.7.1\bin
C:\non-installed\jython2.7.1\Lib
__classpath__
__pyclasspath__/
C:\non-installed\jython2.7.1\Lib\site-packages

C:\Users\Peter Zejda\Google Drive\home\programming\fun\skipads.sikuli>

For whatever reason, the API jar files seems to have trouble finding the ITesseract class.

Peter Zejda (ptkeen) said : #8

Ah, please ignore all of this :-(. Just in case, I re-downloaded both the IDE and the API jar files, this time being sure to pull them down directly from https://launchpad.net/sikuli/+download. And then suddenly, both are working correctly (including the API jar file). So it seems somewhere along the way I must have downloaded a broken version of the jar file (from sourceforge, based on the error message).

Sorry to have troubled you and wasted your time :-\.