Skip to content

Instantly share code, notes, and snippets.

@nandosola
Created August 22, 2012 11:00
Show Gist options
  • Save nandosola/3424416 to your computer and use it in GitHub Desktop.
Save nandosola/3424416 to your computer and use it in GitHub Desktop.
Call Java/Swing from C++/Qt with JNI - requires Oracle Java 7u4 or OpenJDK 7u3 (or higher)
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
public class CreateAndShowGUI extends Thread{
Runnable HelloWorldFrame = new Runnable() {
public void run() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add the ubiquitous "Hello World" label.
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
//Display the window.
frame.pack();
frame.setVisible(true);
}
};
public void run(){
try {
// Being Swing good citizens
SwingUtilities.invokeAndWait(HelloWorldFrame);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class HolaMundoSwing {
public static void main(String[] args) {
CreateAndShowGUI t1=new CreateAndShowGUI();
t1.start();
try {
t1.join();//is more important synchronize the main thread with the gui thread
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#include "jvm.h"
Jvm::Jvm(){
}
void Jvm::create_jvm(){
JavaVM *jvm;
JNIEnv *env;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
//jvm options
options[0].optionString = "-Djava.compiler=NONE";
options[1].optionString = "-Djava.class.path=./javaApp";
options[2].optionString = "";//other options example -verbose:jni
//jvm args
vm_args.version = JNI_VERSION_1_6;
vm_args.nOptions = 3;
vm_args.options = options;
vm_args.ignoreUnrecognized = JNI_TRUE;
jint res = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
if (res < 0) {
qDebug()<<"Can't create Java VM\n";
exit(1);
} else {
invoke_class(env);
jvm->DestroyJavaVM();
}
}
void Jvm::invoke_class(JNIEnv* env) {
jclass cls;
jmethodID mainMethod;
jobjectArray applicationArgs;
jstring applicationArg0,applicationArg1,applicationArg2;
//class and main method
cls = env->FindClass("HolaMundoSwing");//HolaMundoSwing is the name of the class
if (cls == 0) qDebug()<<"Sorry, I can't find the class";
mainMethod = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");//main is the name of the method
//Application arguments array
applicationArgs = env->NewObjectArray(3, env->FindClass("java/lang/String"), NULL);
applicationArg0 = env->NewStringUTF("From");
applicationArg1 = env->NewStringUTF("C");
applicationArg2 = env->NewStringUTF("program");
env->SetObjectArrayElement(applicationArgs, 0, applicationArg0);
env->SetObjectArrayElement(applicationArgs, 1, applicationArg1);
env->SetObjectArrayElement(applicationArgs, 2, applicationArg2);
env->CallStaticVoidMethod(cls, mainMethod, applicationArgs); //Call to the method
}
@trusktr
Copy link

trusktr commented Jan 29, 2014

What does

Jvm::Jvm(){
}

do?

@conicole
Copy link

hum, don't work on my side. Maybe because jvm.h is missing ? I'm not able to fix my problem, if you have it somewhere... It could be cool. It's a really interesting issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment