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. I can redirect output of gzip, but cannot redirect output of java.
Forum Updated to NodeBB v4.3 + New Features

I can redirect output of gzip, but cannot redirect output of java.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.2k 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.
  • Z Offline
    Z Offline
    zzzhhhzzzhhh
    wrote on last edited by
    #1

    It's on Windows 10. The following code can redirect output of gzip to display its help info:

    #include <QCoreApplication>
    #include <QProcess>
    #include <iostream>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        std::cout << "Main process ID: " << a.applicationPid() << "\n";
        QStringList args;
        args << "-h";
        QProcess process;
        process.setProgram("C:\\Program Files (x86)\\GnuWin32\\bin\\gzip.exe");
        process.setArguments(args);
        process.start();
        process.waitForStarted();
        std::cout << "Process started: " << process.processId() << std::endl;
        process.waitForReadyRead();
        std::cout << process.readAll().toStdString() << std::endl;
        process.close();
        return 0;
    }
    

    But if I change gzip to java, i.e., replace the setProgram line to:

    process.setProgram("C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath\\java.exe");
    

    , it is expected that the help info of java be output. But there is no such info printed out in the application's console window. I have double checked that the path is correct and the process ID of java indicates that it is started correctly. So what is wrong with my code? How to fix it? Thank you.

    jsulmJ 1 Reply Last reply
    0
    • Z zzzhhhzzzhhh

      It's on Windows 10. The following code can redirect output of gzip to display its help info:

      #include <QCoreApplication>
      #include <QProcess>
      #include <iostream>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
          std::cout << "Main process ID: " << a.applicationPid() << "\n";
          QStringList args;
          args << "-h";
          QProcess process;
          process.setProgram("C:\\Program Files (x86)\\GnuWin32\\bin\\gzip.exe");
          process.setArguments(args);
          process.start();
          process.waitForStarted();
          std::cout << "Process started: " << process.processId() << std::endl;
          process.waitForReadyRead();
          std::cout << process.readAll().toStdString() << std::endl;
          process.close();
          return 0;
      }
      

      But if I change gzip to java, i.e., replace the setProgram line to:

      process.setProgram("C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath\\java.exe");
      

      , it is expected that the help info of java be output. But there is no such info printed out in the application's console window. I have double checked that the path is correct and the process ID of java indicates that it is started correctly. So what is wrong with my code? How to fix it? Thank you.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @zzzhhhzzzhhh Is it possible that java outputs to stderr?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Z 1 Reply Last reply
      3
      • Gojir4G Offline
        Gojir4G Offline
        Gojir4
        wrote on last edited by SGaist
        #3

        QProcess::readAll() will only read the read channel currently selected, which, I guess, is stdout by default. To read all output including stderr, you need to call also QProcess::readAllStandardError().

            process.waitForReadyRead();
            std::cout << process.readAllStandardOutput().toStdString() << std::endl;
            std::cout << process.readAllStandardError().toStdString() << std::endl;
            process.close();
        

        The disadvantage of this is that you will lose the order of the messages as you will display all standard output messages before error messages.

        So I would recommend using signals QProcess::readyReadStandardOutput() and QProcess::readyReadStandardError() :

        int main(int argc, char *argv[])
        {
            QCoreApplication a(argc, argv);
            std::cout << "Main process ID: " << a.applicationPid() << "\n";
            QStringList args;
            args << "-h";
            QProcess process;
            process.setProgram("C:\\Program Files (x86)\\GnuWin32\\bin\\gzip.exe");
            process.setArguments(args);
        
            QObject::connect(&process, &QProcess::readyReadStandardError, [&process]{
               std::cerr << process.readAllStandardError().toStdString();
            });
        
            QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process]{
               std::cout << process.readAllStandardOutput().toStdString();
            });
        
            process.start();
            process.waitForStarted();
            std::cout << "Process started: " << process.processId() << std::endl;
            process.waitForFinished();
            
            process.close();
            return 0;
        }
        

        [edit: Fixed code SGaist]

        Z 1 Reply Last reply
        6
        • Gojir4G Gojir4

          QProcess::readAll() will only read the read channel currently selected, which, I guess, is stdout by default. To read all output including stderr, you need to call also QProcess::readAllStandardError().

              process.waitForReadyRead();
              std::cout << process.readAllStandardOutput().toStdString() << std::endl;
              std::cout << process.readAllStandardError().toStdString() << std::endl;
              process.close();
          

          The disadvantage of this is that you will lose the order of the messages as you will display all standard output messages before error messages.

          So I would recommend using signals QProcess::readyReadStandardOutput() and QProcess::readyReadStandardError() :

          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
              std::cout << "Main process ID: " << a.applicationPid() << "\n";
              QStringList args;
              args << "-h";
              QProcess process;
              process.setProgram("C:\\Program Files (x86)\\GnuWin32\\bin\\gzip.exe");
              process.setArguments(args);
          
              QObject::connect(&process, &QProcess::readyReadStandardError, [&process]{
                 std::cerr << process.readAllStandardError().toStdString();
              });
          
              QObject::connect(&process, &QProcess::readyReadStandardOutput, [&process]{
                 std::cout << process.readAllStandardOutput().toStdString();
              });
          
              process.start();
              process.waitForStarted();
              std::cout << "Process started: " << process.processId() << std::endl;
              process.waitForFinished();
              
              process.close();
              return 0;
          }
          

          [edit: Fixed code SGaist]

          Z Offline
          Z Offline
          zzzhhhzzzhhh
          wrote on last edited by
          #4

          @Gojir4 said in I can redirect output of gzip, but cannot redirect output of java.:

          process.waitForFinished();

          Thank you for the reply. But there are some syntax errors:

          alt text
          It's beyond my capability to fix them, so could you please help me with it? Thanks.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi,

            The sample has been updated.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            Gojir4G Z 2 Replies Last reply
            3
            • SGaistS SGaist

              Hi,

              The sample has been updated.

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #6

              @SGaist Thank you

              1 Reply Last reply
              0
              • SGaistS SGaist

                Hi,

                The sample has been updated.

                Z Offline
                Z Offline
                zzzhhhzzzhhh
                wrote on last edited by
                #7

                @SGaist Thank you so much for your help!

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @zzzhhhzzzhhh Is it possible that java outputs to stderr?

                  Z Offline
                  Z Offline
                  zzzhhhzzzhhh
                  wrote on last edited by
                  #8

                  @jsulm So according to Gojir4's code, java outputs to stderr (at least for -h).

                  1 Reply Last reply
                  1

                  • Login

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