Note that there are some explanatory texts on larger screens.

plurals
  1. POJava JNI: Creating a Swing Window using JNI from C
    primarykey
    data
    text
    <p>I'm using JNI to invoke a static java method which in turn creates a Swing JFrame and displays it. The code is fairly simple, and the Java-code is working standalone (i.e. <code>java StartAWT</code> does what it should) whereas when called from C using JNI the process hangs.</p> <p>I'm using the JDK 1.7.0_09 on Mac OS X 10.8 Mountain Lion.</p> <p>This is the C code I'm using to invoke the static method:</p> <pre class="lang-c prettyprint-override"><code>JavaVM* jvm; JNIEnv* env = create_vm(&amp;jvm); jclass class = (*env)-&gt;FindClass(env, "StartAWT"); jmethodID method = (*env)-&gt;GetStaticMethodID(env, class, "run", "()V"); (*env)-&gt;CallStaticVoidMethod(env, class, method); (*jvm)-&gt;DestroyJavaVM(jvm); </code></pre> <p>The <code>StartAWT</code> class looks like this:</p> <pre class="lang-java prettyprint-override"><code>public class StartAWT { public static class Starter implements Runnable { public void run() { System.out.println("Runnning on AWT Queue."); JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("That's a frame!"); JLabel label = new JLabel("A Label"); frame.getContentPane().add(label); frame.pack(); frame.setVisible(true); } } public static class GUI implements Runnable { public void run() { try { System.out.println("Going to put something on the AWT queue."); SwingUtilities.invokeAndWait(new Starter()); } catch (Exception exc) { throw new RuntimeException(exc); } } } public static void run() { Thread gui = new Thread(new GUI()); gui.start(); } } </code></pre> <p>When I start the application, I do see <code>Going to put something on the AWT queue</code> but not <code>Running on AWT Queue</code>.</p> <p>I believe that the Virtual Machine inside my C Process does not have an AWT event queue but I don't know how to set it up for having one either (nor am I sure that this is the reason).</p> <p>What is to be done in order to show an AWT based GUI using JNI ?</p> <p>--</p> <p><strong>EDIT:</strong> I've inserted loops to see which threads are alive and which are not (can be seen in <a href="https://gist.github.com/4697138" rel="nofollow noreferrer">this gist</a>). In this version I do the invocation of <code>SwingUtilities.invokeAndWait</code> in another thread. The result: The main thread is alive (C). The first thread dispatched by Java (not the main thread) is alive; the thread doing the Call <code>invokeAndWait</code> is blocked (I don't think that invokeAndWait did even return), the function which should be run on the EventQueue is not even entered.</p> <p>I've also tried invoking <code>SwingUtilities.invokeAndWait</code> directly, which will give the following message:</p> <pre class="lang-none prettyprint-override"><code>2013-02-02 13:50:23.629 swing[1883:707] Cocoa AWT: Apple AWT Java VM was loaded on first thread -- can't start AWT. ( 0 liblwawt.dylib 0x0000000117e87ad0 JNI_OnLoad + 468 1 libjava.dylib 0x00000001026076f1 Java_java_lang_ClassLoader_00024NativeLibrary_load + 207 2 ??? 0x000000010265af90 0x0 + 4335185808 ) </code></pre> <p>This is also what I've read in other questions here on StackOverflow, such as the one suggested in the comments below. However, I could not find a solution to the original problem. Maybe it is worth noting that after the above message came up the main thread is still alive, i.e. neither did the process deadlock nor crash.</p> <p>--</p> <p><strong>EDIT:</strong> I tested the code on Linux where it is working as expected. So I believe this is a Mac OS X issue with Cocoa AWT, but I don't know how to circumvent it.</p> <p>--</p> <p><strong>EDIT:</strong> I also tried moving the entire invocation of the JVM onto a new native thread. This works on Mac OS X 10.6 with Apples Java 32-bit (1.6.0_37), but results in the same deadlock as described above. On Mac OS X 10.8 this is worse, the application crases with the only message "Trace/BPT trap: 5" (which <a href="https://stackoverflow.com/questions/14675206/mac-os-x-what-is-trace-bpt-trap-5">seems to be related to loading dynamic libraries</a>).</p> <p>I also tried bundling the binary as described <a href="https://stackoverflow.com/questions/1596945/building-osx-app-bundle">in this Q&amp;A</a>, but the launch fails with the message <code>lsopenurlswithrole() failed with the message -10810</code>, which is an unknown error, according to Apples <a href="https://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/LaunchServicesReference.pdf" rel="nofollow noreferrer">Launch Services Reference</a>. The latter also happens without attempting to use AWT (the mere JVM invocation fails).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload