Skip to content

Calling Swing/AWT components with JavaCall on OS X #32

Open

Description

@StreetLevel

Hi there,

i would like to call swing/awt components with JavaCall. But there seems to be some thread issues. Frames do not open.
I think this is not an JavaCall issue but an cocoa/JNI thing but maybe someone knows a workaround for JavaCall on OS X?

Thank you in advance.

Best Max

Activity

  1. aviks commented on Apr 24, 2016

    @aviks
    Collaborator

    I've investigated a little bit, and couldn't come up with any answers. It would be good to get this working.

  2. jarnoharno commented on Sep 13, 2016

    @jarnoharno
    Contributor

    I ran into this issue too. It occurs even if I just call things like ImageIO.getCacheDirectory(): a GUI application opens and Julia hangs.

  3. aviks commented on Sep 13, 2016

    @aviks
    Collaborator

    Try starting the JVM with "-Djava.awt.headless=true". That will not make graphics work, but might help with the hang.

  4. jarnoharno commented on Sep 13, 2016

    @jarnoharno
    Contributor

    That did it, thanks a lot!

  5. mkitti commented on May 4, 2020

    @mkitti
    Member

    Is there still a problem on OS X? I do not have a problem on Linux.

  6. hx2A commented on Feb 17, 2021

    @hx2A

    FWIW, the Python - Java bridge library jpype also seems to have problems with Swing/AWT on OSX. Something about the threading? I don't understand the reasons fully, as I don't have access to a Mac or do much Java GUI programming. Still, it's reasonable that the issue here has the same underlying cause.

    Here are two issues with discussions that might be helpful to you:

    jpype-project/jpype#906
    jpype-project/jpype#911

    And separately, does this library work on Java 11? If not, are there plans to support it in the future?

  7. mkitti commented on Feb 17, 2021

    @mkitti
    Member

    This library does work on Java 11.

  8. hx2A commented on Feb 17, 2021

    @hx2A

    Interesting. The documentation seemed to imply it was only tested on versions 7, 8, and 9.

    I'm happy this library exists and will start using it.

  9. mkitti commented on Feb 17, 2021

    @mkitti
    Member

    Actually, the way I would say it is that the minimum version is now Java 8:

            vm_args = JavaVMInitArgs(JNI_VERSION_1_8, convert(Cint, length(opts)),
                                     convert(Ptr{JavaVMOption}, pointer(opt)), JNI_TRUE)
    

    In the JPype issue, did they find a solution? I do not have a Mac to test this code on. My suggestion would be to use interprocess communication rather than in-process interop to get around the GUI issues. For example:
    https://github.com/jbytecode/juliacaller

    Another idea would be to use ZeroMQ (ZMQ) to drive the application.

  10. hx2A commented on Feb 17, 2021

    @hx2A

    No, the JPype issue has not been fixed. They don't have a Mac to test on, and neither do I.

    Interprocess communication would be slower, and not worth it because doing it in-process is most likely possible. There is information in the discussion for those other two issues that might provide hints for what needs to be done to fix this problem with this library.

  11. vsquared commented on Jun 8, 2024

    @vsquared

    The following runs on my Mac (Sonoma 14.4.1, Intel). The code was run in a Thonny editor with PyObjc plugin.

    import jpype
    import jpype.imports
    
    jpype.startJVM()
    import java
    import javax
    from javax.swing import *
    from Cocoa import NSApp
    
    def createAndShowGUI():
        frame = JFrame("HelloWorldSwing")
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
        label = JLabel("Hello World")
        frame.getContentPane().add(label)
        frame.pack()
        frame.setVisible(True)
    
    javax.swing.SwingUtilities.invokeLater(createAndShowGUI)
    NSApp.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions