Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
StreetLevel opened this issue Mar 11, 2016 · 11 comments
Open

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

StreetLevel opened this issue Mar 11, 2016 · 11 comments
Labels

Comments

@StreetLevel
Copy link

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

@aviks
Copy link
Collaborator

aviks commented Apr 24, 2016

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

@jarnoharno
Copy link
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.

@aviks
Copy link
Collaborator

aviks commented Sep 13, 2016

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

@jarnoharno
Copy link
Contributor

That did it, thanks a lot!

@mkitti
Copy link
Member

mkitti commented May 4, 2020

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

@mkitti mkitti added the bug label May 4, 2020
@hx2A
Copy link

hx2A commented Feb 17, 2021

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?

@mkitti
Copy link
Member

mkitti commented Feb 17, 2021

This library does work on Java 11.

@hx2A
Copy link

hx2A commented Feb 17, 2021

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.

@mkitti
Copy link
Member

mkitti commented Feb 17, 2021

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.

@hx2A
Copy link

hx2A commented Feb 17, 2021

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.

@vsquared
Copy link

vsquared commented Jun 8, 2024

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
Labels
Projects
None yet
Development

No branches or pull requests

6 participants