Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Independant Threads with separate CommandLine
Forum Updated to NodeBB v4.3 + New Features

[Solved] Independant Threads with separate CommandLine

Scheduled Pinned Locked Moved General and Desktop
18 Posts 5 Posters 6.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    huckfinn
    wrote on last edited by
    #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

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Why don't you start the executable using [[Doc:QProcess]]? This gives you an asynchronous for free.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        But if you want to run with another cmd.exe why don't you use your main as a wrapper for the java process and then use QProcess to start such main?

        1 Reply Last reply
        0
        • H Offline
          H Offline
          huckfinn
          wrote on last edited by
          #4

          I wonder If can run that in a separate shell?

          @
          QString program = "C:\Program Files\Java\jdk1.7.0\bin\java.exe";
          QStringList arguments;
          arguments << "-jar" << "C:\Project\dist\Project.jar" << "-m" << "module.db" << "-i" << "K;1076076896,1076076882" ;
          QProcess *myProcess = new QProcess();
          myProcess->start(program, arguments);
          @

          What can I search for, when I want to use the wrapper possibility?

          1 Reply Last reply
          0
          • G Offline
            G Offline
            goetz
            wrote on last edited by
            #5

            First:
            Don't use backslashes for the program path, use forward slashes. Qt will do the translation transparently.

            Second:
            Why do you want to involve a shell? It will do nothing else than calling java.exe with your arguments! Does the code you presented not work?

            http://www.catb.org/~esr/faqs/smart-questions.html

            1 Reply Last reply
            0
            • H Offline
              H Offline
              huckfinn
              wrote on last edited by
              #6

              The code in my first post works, I can invoke my extarnal.jar from my Qt-Project.

              But my external.jar continuously listens on the socket to a port, while the expected message is never being sent by my suspended Qt-Project.

              Therefore I try to make run the external.jar indipendent.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #7

                I meant the second snippet, using QProcess. Does that work?

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • H Offline
                  H Offline
                  huckfinn
                  wrote on last edited by
                  #8

                  According to the TaskManager a java.exe appears just for 1-2 seconds and disappears, but the jar is not executed properly.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    goetz
                    wrote on last edited by
                    #9

                    And it does work from the command line itself? How about giving a complete path to your module.db - it's very likely that it is not found by your java application.

                    http://www.catb.org/~esr/faqs/smart-questions.html

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      huckfinn
                      wrote on last edited by
                      #10

                      Yes, when I call "java -jar external.jar -m modules.db ... " it works.

                      I have catched in external.jar already if that modules.db file would not exist, it would throw an FileNotFound-Exception; but I'll try it.

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        huckfinn
                        wrote on last edited by
                        #11

                        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()
                        {
                        QString t_exe_name("java.exe");
                        QStringList t_exe_arguments;
                        t_exe_arguments << "-jar" << "C:/SD_Projectz/TestServer/dist/TestServer.jar";

                        QProcess *myProcess = new QProcess(p_ToObject);
                        QString t_joined = t_exe_arguments.join(" ");
                        QString t_all = QString("%1 %2").arg(t_exe_name).arg(t_joined);
                        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?

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          goetz
                          wrote on last edited by
                          #12

                          Why do you expect a cmd.exe running in case you start a java.exe?

                          http://www.catb.org/~esr/faqs/smart-questions.html

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            huckfinn
                            wrote on last edited by
                            #13

                            These *.jars have System.out.println("kljdas"); so without any further cmd.exe nothing is visible?

                            Then the only way I recognize the running jar is the java.exe in the TaskManager?

                            And since the jar is running continuously, I would say my java.exe should not disappear?

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              Your java.exe does not "disappear". It has no user interface, so it runs in the background, where it belongs.

                              If you want to see the output of stdout or stderr, you should write a .bat file. Either this opens a cmd.exe automatically when run, or you have to give it as argument to cmd.exe.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                Better yet: if you want to display the output, why not make your Qt application display it instead in some nicely integrated way? QProcess gives you access to the stdout and stderr outputs of the process, after all.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mrtc3
                                  wrote on last edited by
                                  #16

                                  To start in a separate shell try to detach the Process. Then you have your own cmd.exe:
                                  @myProcess->startDetached(prog_name, prog_arguments);@
                                  But then you have no access to its stdio..

                                  hth!

                                  There are 10 types. Those who understand binary and those who don't .)

                                  1 Reply Last reply
                                  0
                                  • F Offline
                                    F Offline
                                    fluca1978
                                    wrote on last edited by
                                    #17

                                    [quote author="huckfinn" date="1317727677"]
                                    But my external.jar continuously listens on the socket to a port, while the expected message is never being sent by my suspended Qt-Project.[/quote]

                                    If I get it right, you need to start the process in background, since it is a daemon, then startdetached should work for you. If you need to check only the output, you can redirect stdout to a log file (or embed a logging facility in your java application).

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      huckfinn
                                      wrote on last edited by
                                      #18

                                      Yes, startDetached() works...

                                      Thank you!

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved