pyjnius icon indicating copy to clipboard operation
pyjnius copied to clipboard

importing AWT classes hangs the script on OSX

Open codeanticode opened this issue 9 years ago • 3 comments

This python script:

from jnius import autoclass
print "hello jnius"
Frame = autoclass("java.awt.Frame")
print "bye jnius"

hangs when loading the Frame class (runnning OSX 10.9.5, JDK 1.8.0_31). I'm aware of issues with AWT on OSX https://github.com/kivy/pyjnius/issues/85, where a suggested workaround is to use -Djava.awt.headless=true.

But if I explicitly don't want to use the headless mode, is there ant workaround to this issue?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

codeanticode avatar Mar 02 '15 23:03 codeanticode

I had the same problem and here is a solution/workaround. For AWT to work, the cocoa main loop needs to be started. This can be done outside jnius. Something like this works for me:

import objc
from Foundation import *
from AppKit import *
from PyObjCTools import AppHelper

def runAwtStuff():
    # main program goes here

class AppDelegate (NSObject):
    def runjava_(self,arg):
        runAwtStuff()

    def applicationDidFinishLaunching_(self, aNotification):
        self.performSelectorInBackground_withObject_("runjava:",0)

def main():
    app = NSApplication.sharedApplication()
    delegate = AppDelegate.alloc().init()
    NSApp().setDelegate_(delegate)
    AppHelper.runEventLoop()

if __name__ == '__main__' : main()

tpietzsch avatar Mar 13 '17 17:03 tpietzsch

@tpietzsch many thanks for sharing the workaround! I'm not using Pyjnius at the moment, but will give it a try as soon as I have a chance.

codeanticode avatar Mar 14 '17 20:03 codeanticode

@tpietzsch The workaround works great! For anyone who also stumbles upon this issue: the needed lib can be installed via

pip install -U pyobjc

frederikschubert avatar Apr 16 '20 08:04 frederikschubert