[Solved] Independant Threads with separate CommandLine
Page |
1 |
Hello,
I have a main Qt-Thread (main.exe), which in turn starts an executable jar via JNI (lets say external.jar).
This external.jar listens continuously on a special socket(localhost, PORT).
The expected message should be sent by the main.exe, which is blocked by the Listening external.jar.
I would like to run that jar in a separate thread, and if possible with a separate cmd.exe.
- #include <jni.h>
- #include "ExternJar.h"
- #pragma comment (lib,"C:\\Program Files\\Java\\jdk1.6.0_26\\lib\\jvm.lib")
- /// Constructor
- ExternJar::ExternJar(const char* pPathToJar, const char* pPathToLib, const char *t_argv, const char *t_path2Package, const char* t_methodName)
- {
- int oi = 0;
- options[oi++].optionString = const_cast<char*>(pPathToJar);
- vm_args.nOptions = oi;
- vm_args.version = JNI_VERSION_1_6;
- vm_args.options = options;
- vm_args.ignoreUnrecognized = JNI_FALSE;
- argumentString = t_argv;
- path2Jar = pPathToJar;
- path2Lib = pPathToLib;
- packagePath = t_path2Package;
- methodName = t_methodName;
- debugMode = 0;
- }
- void ExternJar::setClassName()
- {
- switch(JNI_CreateJavaVM( &jvm,(void **)&env, &vm_args)) {
- case JNI_OK:
- printf("\nJVM created --> Ready ...\n");
- }
- if( NULL == (cls = env->FindClass(packagePath)) ) printf("\nCan't find class %s\n", packagePath);
- else printf("\nClass %s found!\n", packagePath);
- }
- void ExternJar::setArgCV()
- {
- if (!argumentString) return;
- this->applicationArg0 = env->NewStringUTF(argumentString);
- this->applicationArgs = env->NewObjectArray(1, env->FindClass("java/lang/String"), applicationArg0);
- env->SetObjectArrayElement(applicationArgs, 0, applicationArg0);
- }
- void ExternJar::setMethodName()
- {
- mid = env->GetStaticMethodID(cls, methodName, "([Ljava/lang/String;)V");
- printf("\nMethod name set to: %s", methodName);
- }
- void ExternJar::callXMethod()
- {
- printf("\nCall now \n%s(\"%s\"):", methodName, argumentString);
- if (mid != NULL) env->CallStaticVoidMethod(cls, mid, applicationArgs);
- else printf("\nMethod %s corrupted!\n", mid);
- }
- JavaVM * ExternJar::getVM()
- {
- return this->jvm;
- }
- void * ExternJar::getEnv()
- {
- return this->env;
- }
- bool ExternJar::destroyVM()
- {
- printf("\nDestroy VM now!");
- if ( this->getVM()->DestroyJavaVM() == 1 ) return true;
- else return false;
- }
- void ExternJar::run()
- {
- this->setClassName();
- this->setArgCV();
- this->setMethodName();
- this->callXMethod();
- exec();
- }
I have Q_OBJECT set in the Header, it derives from QThread. I have a run() Method which also has a exec()… What else?
Thank you for any hints.
Cheers Huck
17 replies
Why don’t you start the executable using QProcess? This gives you an asynchronous for free.
I wonder If can run that in a separate shell?
- QStringList arguments;
- arguments << "-jar" << "C:\\Project\\dist\\Project.jar" << "-m" << "module.db" << "-i" << "K;1076076896,1076076882" ;
- myProcess->start(program, arguments);
What can I search for, when I want to use the wrapper possibility?
Here I call another *.jar, Its a Server, which listens uninterrupted to a socket and prints status messages System.out.println(“Bla”) continously as well. This jar has an infinte while-loop and does not terminate..
SO when I start this, another cmd.exe opens and that output should be visible?
- void MyClass::runExternal()
- {
- QStringList t_exe_arguments;
- t_exe_arguments << "-jar" << "C:/SD_Projectz/TestServer/dist/TestServer.jar";
- myProcess->start(t_all);
- printf("\nWAIT: %d", myProcess->pid());
- }
Each start() my pid() is changed, visible on my Parent command line window, but no other cmd.exe opens for this Process.
When p_ToObject (the parent) is deleted, my process is terminated as well, right?
You must log in to post a reply. Not a member yet? Register here!


