Imports not working when executing Sikuli/Jython from Java via ObjectFactory

Asked by Daniel Kurtz

So, I'm trying to put a Swing GUI in front of my Sikuli script. I've got the basic script working (without the GUI) in Eclipse. With the GUI added, I've defined a Java interface for the script, and on clicking a button in the GUI:

   public void mouseClicked(MouseEvent arg0) {
          JythonFactory jf = JythonFactory.getInstance();
          EpicPolicyPusherType eType = (EpicPolicyPusherType) jf.getJythonObject(
                                 "jyinterface.interfaces.EpicPolicyPusherType",
                                 "src/EpicPolicyPusher.py");
   }

The object factory is defined as:

   public static Object getJythonObject(String interfaceName,
                                        String pathToJythonModule){

       Object javaInt = null;
       PythonInterpreter interpreter = new PythonInterpreter();
       interpreter.execfile(pathToJythonModule);
       ........

There is further code in the object factory (I copied it from a tutorial) but it's not relevant at this point because we're not getting passed the interpreter.execfile command.

The script import Sikuli at the top, as instructed here:https://github.com/RaiMan/SikuliX-2014/wiki/Usage-in-Java-programming

     import time, datetime
     import org.sikuli.script.SikulixForJython
     from sikuli import *

For the record, I've added the sikulixapi.jar library to both the Java build path and the PYTHONPATH. Jython version BTW is 2.7. %SIKULI_HOME% is pointing to c:\sikulix.

Anyway, when I try to run, I get the GUI. I click the button. The object factory instantiates the script object, which tries to initialize, and throws:

Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "src/EpicPolicyPusher.py", line 20, in <module>
    from sikuli import *
  File "C:\SikuliX\sikulixapi.jar\Lib\sikuli\__init__.py", line 6, in <module>
  File "C:\SikuliX\sikulixapi.jar\Lib\sikuli\Sikuli.py", line 32, in <module>
java.lang.ExceptionInInitializerError
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)
 at org.python.core.Py.loadAndInitClass(Py.java:991)
 at org.python.core.Py.findClassInternal(Py.java:926)
 at org.python.core.Py.findClassEx(Py.java:977)
 at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133)
 at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:33)
 at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122)
 at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:134)
 at org.python.core.PyObject.__findattr__(PyObject.java:946)
 at org.python.core.imp.importFromAs(imp.java:1160)
 at org.python.core.imp.importFrom(imp.java:1132)
 at sikuli.Sikuli$py.f$0(C:\SikuliX\sikulixapi.jar\Lib\sikuli\Sikuli.py:547)
 at sikuli.Sikuli$py.call_function(C:\SikuliX\sikulixapi.jar\Lib\sikuli\Sikuli.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:167)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.imp.createFromCode(imp.java:436)
 at org.python.core.util.importer.importer_load_module(importer.java:109)
 at org.python.modules.zipimport.zipimporter.zipimporter_load_module(zipimporter.java:163)
 at org.python.modules.zipimport.zipimporter$zipimporter_load_module_exposer.__call__(Unknown Source)
 at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:46)
 at org.python.core.imp.loadFromLoader(imp.java:587)
 at org.python.core.imp.find_module(imp.java:537)
 at org.python.core.PyModule.impAttr(PyModule.java:106)
 at org.python.core.imp.import_next(imp.java:842)
 at org.python.core.imp.import_module_level(imp.java:959)
 at org.python.core.imp.importName(imp.java:1062)
 at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
 at org.python.core.PyObject.__call__(PyObject.java:431)
 at org.python.core.__builtin__.__import__(__builtin__.java:1232)
 at org.python.core.imp.importAll(imp.java:1182)
 at sikuli$py.f$0(C:\SikuliX\sikulixapi.jar\Lib\sikuli\__init__.py:6)
 at sikuli$py.call_function(C:\SikuliX\sikulixapi.jar\Lib\sikuli\__init__.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:167)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.imp.createFromCode(imp.java:436)
 at org.python.core.util.importer.importer_load_module(importer.java:109)
 at org.python.modules.zipimport.zipimporter.zipimporter_load_module(zipimporter.java:163)
 at org.python.modules.zipimport.zipimporter$zipimporter_load_module_exposer.__call__(Unknown Source)
 at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:46)
 at org.python.core.imp.loadFromLoader(imp.java:587)
 at org.python.core.imp.find_module(imp.java:537)
 at org.python.core.imp.import_next(imp.java:840)
 at org.python.core.imp.import_module_level(imp.java:959)
 at org.python.core.imp.importName(imp.java:1062)
 at org.python.core.ImportFunction.__call__(__builtin__.java:1280)
 at org.python.core.PyObject.__call__(PyObject.java:431)
 at org.python.core.__builtin__.__import__(__builtin__.java:1232)
 at org.python.core.imp.importAll(imp.java:1182)
 at org.python.pycode._pyx2.f$0(src/EpicPolicyPusher.py:412)
 at org.python.pycode._pyx2.call_function(src/EpicPolicyPusher.py)
 at org.python.core.PyTableCode.call(PyTableCode.java:167)
 at org.python.core.PyCode.call(PyCode.java:18)
 at org.python.core.Py.runCode(Py.java:1386)
 at org.python.core.__builtin__.execfile_flags(__builtin__.java:535)
 at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:286)
 at jyinterface.factory.JythonFactory.getJythonObject(JythonFactory.java:22)
 at PolicyMaker_UI.UpFront$2.mouseClicked(UpFront.java:362)
 at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
 at java.awt.Component.processMouseEvent(Unknown Source)
 at javax.swing.JComponent.processMouseEvent(Unknown Source)
 at java.awt.Component.processEvent(Unknown Source)
 at java.awt.Container.processEvent(Unknown Source)
 at java.awt.Component.dispatchEventImpl(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
 at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
 at java.awt.Container.dispatchEventImpl(Unknown Source)
 at java.awt.Window.dispatchEventImpl(Unknown Source)
 at java.awt.Component.dispatchEvent(Unknown Source)
 at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
 at java.awt.EventQueue.access$500(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.awt.EventQueue$3.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.awt.EventQueue$4.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
 at java.awt.EventQueue.dispatchEvent(Unknown Source)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
 at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
 at java.awt.Robot.checkNotDispatchThread(Unknown Source)
 at java.awt.Robot.waitForIdle(Unknown Source)
 at org.sikuli.script.Mouse.move(Mouse.java:345)
 at org.sikuli.script.Mouse.move(Mouse.java:318)
 at org.sikuli.script.Mouse.init(Mouse.java:59)
 at org.sikuli.script.Screen.initScreens(Screen.java:89)
 at org.sikuli.script.Screen.<clinit>(Screen.java:58)
 ... 90 more
java.lang.ExceptionInInitializerError: java.lang.ExceptionInInitializerError

So, the import of standard Python/Jython modules works OK, the import of SikulixForJython works, the main "from sikuli import *" dies gruesomely.

For the record, since the named import of SikulixForJython works, I've experimented with explicit import statements, like:
     from org.sikuli.script import Region

These imports do not fail, but any attempt to use the named area of functionality e.g. "x = Region(1,1,1000,1000)" will fail at runtime with 'variable "Region" not defined' errors.

Any assistance appreciated.

d

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

in Germany we have a saying:
You do not see the wood because of all trees.

rather easy and obvious:
Caused by: java.lang.IllegalThreadStateException: Cannot call method from the event dispatcher thread
 at java.awt.Robot.checkNotDispatchThread(Unknown Source)

.. a well known problem with Swing and AWT:
to use actions like click which are based on Java awt Robot class you have to process the tasks under the hood of the Java event dispatch thread, meaning, that you cannot use click in code, that is triggered in a button handling method.

There are tons of information in the net about the background and possible solutions.

The general guideline is, to take care somehow, that the mouseClicked event handler returns as soon as possible, without doing anything that conflicts with the event dispatcher (e.g. run the script in a completely separated process).

Can you help with this problem?

Provide an answer of your own, or ask Daniel Kurtz for more information if necessary.

To post a message you must log in.