question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

macOS: Python-to-Java bridge JPype1 (1.2.0) not working (Java problem)

See original GitHub issue

Original problem hint on LaunchPad

from jpype import *
jvm = getDefaultJVMPath()
startJVM(jvm, classpath = '/Users/raimundhocke/IdeaProjects/_SUPPORT/_Latest/2_0_4/sikulixapi-2.0.4.jar')
Screen = JClass('org.sikuli.script.Screen')
s = Screen() # here it hangs trying to get the default Screen object
print(s)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RaiMancommented, Dec 18, 2020

@adrian-evo

Excellent work !!! I will have a look into it and will come back, since I am planning, to release a SikuliX version next year, that can be used from Python. JPype is one candidate and py4j the other one.

about the JPype-macOS-SikuliX problem I will dive into the next days. Thanks for your feedback.

0reactions
Thrameoscommented, Jul 15, 2021

My general understanding of this issue is that osx has interactions between threads leading to a deadlock between the main and gui threads. This does not occur on linux or windows because they place no restriction on the threading and servicing the GUI. Unfortunately I have no access to an osx machine so I have never been able to replicate it.

Assuming that it can be replicated, the solution I would try is to change the architecture in jpype/native/common/jp_context.cpp such that the the start up creates a separate thread for launching the JVM and the shutdown declares the python main thread as a daemon thread (which makes it so that the Python thread is not longer a hold up for shutdown) and then passes a message to the Java main thread so that the real shutdown can be called. Current JPype and pyjnius are both structured such that the Java and Python share the same main thread which is supposed to result in a deadlock on osx. There was a work around in jpype/_gui.py

def setupGuiEnvironment(cb):
    if _sys.platform == 'darwin':
        from PyObjCTools import AppHelper
        m = {'run': cb}
        proxy = _jproxy.JProxy('java.lang.Runnable', m)
        cbthread = _jclass.JClass("java.lang.Thread")(proxy)
        cbthread.start()
        AppHelper.runConsoleEventLoop()
    else:
        cb()

Unfortunately there is no documentation of how the workaround around was supposed to be used so I can’t give any guidance. My guess is that in this a thread which likely is the main application is launched from within Java (which make it say the Python main is now a sub process, and the actual Python main is now transferred to running the AppHelper event loop. I don’t really like the structure of this fix as for use in interactive shells and such you really want the Python main thread to be free to interact and have the JVM and AppHelper serviced by the separate worker threads.

Alternative jpype/_core.py could be changed create the threads and manage them within the Python (rather than C++). But again until I can replicate the deadlock and be able to resolve the resulting shutdown issues, I am going to be stuck trying to give guidance to others.

Both Python and Java have the magical concept of a main thread. The main thread is the one that services system calls and such. In Java the main thread is the one that was used to call the startup routine and is the only thread that can call a shutdown (without creating a deadlock) as the shutdown waits for all non daemon threads to terminate before proceeding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JPype1 0.5.7 - PyPI
A Python to Java bridge. ... pip install JPype1==0.5.7 ... Fix: Conversion check for unsigned types did not work in array setters (tautological...
Read more >
JPype Documentation
JPype is not the only Python module of its kind that acts as a bridge to Java. Depending on your programming requirements, one...
Read more >
ModuleNotFoundError: No module named 'jpype'
and I did not get any error during installation. conda install -c conda-forge jpype1 Collecting package metadata: done Solving environment: done ...
Read more >
SikuliX1 - Bountysource
I was using Sikuli API with Java 8 and Maven on a macOS, Windows and Linux ... macOS: Python-to-Java bridge JPype1 (1.2.0) not...
Read more >
main - Anaconda repo
Package Latest Version Doc Dev License linux‑64 osx‑64 win‑64 7za 920 doc LGPL X 7zip 19.00 dev LGPL‑2.1‑or‑later X _anaconda_depends 2022.05 doc dev BSD X X...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found