Created
February 2, 2013 12:42
-
-
Save scravy/4697138 to your computer and use it in GitHub Desktop.
AWT started from JNI (does not work, using Mac OS X)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
JAVAHOME=/Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home | |
swing: swing.c StartAWT.class | |
gcc -Wall -pedantic --std=c99 swing.c -ljvm \ | |
-L$(JAVAHOME)/jre/lib/server \ | |
-I$(JAVAHOME)/include \ | |
-I$(JAVAHOME)/include/darwin \ | |
-o $@ | |
install_name_tool -add_rpath $(JAVAHOME)/jre/lib/server $@ | |
StartAWT.class: StartAWT.java | |
javac StartAWT.java | |
clean: | |
rm -rf *.o swing | |
.PHONY: clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.swing.*; | |
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."); | |
new Thread(new Runnable() { | |
public void run() { | |
System.out.println("Hey there!"); | |
SwingUtilities.invokeLater(new Starter()); | |
} | |
}).start(); | |
long i = 0; | |
for (;;) { | |
System.out.printf("Thread still alive (%d)\n", i++); | |
try { | |
Thread.sleep(5000); | |
} catch (Exception exc) { | |
throw new RuntimeException(exc); | |
} | |
} | |
} catch (Exception exc) { | |
throw new RuntimeException(exc); | |
} | |
} | |
} | |
public static void run() { | |
Thread gui = new Thread(new GUI()); | |
gui.start(); | |
} | |
public static void main(String... args) { | |
run(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include "jni.h" | |
JNIEnv* | |
create_vm(JavaVM ** jvm) | |
{ | |
JNIEnv* env; | |
JavaVMInitArgs vm_args; | |
vm_args.version = JNI_VERSION_1_6; | |
vm_args.nOptions = 0; | |
vm_args.ignoreUnrecognized = 0; | |
if (JNI_CreateJavaVM(jvm, (void**) &env, &vm_args) < 0) { | |
return NULL; | |
} | |
return env; | |
} | |
int main(int argc, char** argv) | |
{ | |
JavaVM* jvm; | |
JNIEnv* env = create_vm(&jvm); | |
jclass class = (*env)->FindClass(env, "StartAWT"); | |
jmethodID method = (*env)->GetStaticMethodID(env, class, "run", "()V"); | |
printf("So far everything worked.\n"); | |
(*env)->CallStaticVoidMethod(env, class, method); | |
printf("That's it, done!\n"); | |
for (unsigned long i = 0;; i++) { | |
printf("Main Thread is alive (%lu)\n", i); | |
sleep(5); | |
} | |
(*jvm)->DestroyJavaVM(jvm); | |
printf("Done.\n"); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment