JavaCall.jl icon indicating copy to clipboard operation
JavaCall.jl copied to clipboard

Calling Swing/AWT components with JavaCall on OS X

Open StreetLevel opened this issue 9 years ago • 11 comments

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

StreetLevel avatar Mar 11 '16 08:03 StreetLevel

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

aviks avatar Apr 24 '16 15:04 aviks

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

jarnoharno avatar Sep 13 '16 12:09 jarnoharno

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

aviks avatar Sep 13 '16 13:09 aviks

That did it, thanks a lot!

jarnoharno avatar Sep 13 '16 14:09 jarnoharno

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

mkitti avatar May 04 '20 00:05 mkitti

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:

https://github.com/jpype-project/jpype/issues/906 https://github.com/jpype-project/jpype/issues/911

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

hx2A avatar Feb 17 '21 15:02 hx2A

This library does work on Java 11.

mkitti avatar Feb 17 '21 19:02 mkitti

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.

hx2A avatar Feb 17 '21 20:02 hx2A

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.

mkitti avatar Feb 17 '21 20:02 mkitti

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.

hx2A avatar Feb 17 '21 21:02 hx2A

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()

vsquared avatar Jun 08 '24 17:06 vsquared