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 Update on Monday, May 27th 2025

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.1k Views
  • 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 14 May 2018, 06:11 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.

    J 1 Reply Last reply 14 May 2018, 06:40
    0
    • Z zzzhhhzzzhhh
      14 May 2018, 06:11

      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.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 14 May 2018, 06:40 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 15 May 2018, 20:02
      3
      • G Offline
        G Offline
        Gojir4
        wrote on 14 May 2018, 08:37 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 14 May 2018, 21:23
        6
        • G Gojir4
          14 May 2018, 08:37

          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 14 May 2018, 21:23 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 14 May 2018, 21:27 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

            G Z 2 Replies Last reply 15 May 2018, 06:05
            3
            • SGaistS SGaist
              14 May 2018, 21:27

              Hi,

              The sample has been updated.

              G Offline
              G Offline
              Gojir4
              wrote on 15 May 2018, 06:05 last edited by
              #6

              @SGaist Thank you

              1 Reply Last reply
              0
              • SGaistS SGaist
                14 May 2018, 21:27

                Hi,

                The sample has been updated.

                Z Offline
                Z Offline
                zzzhhhzzzhhh
                wrote on 15 May 2018, 19:59 last edited by
                #7

                @SGaist Thank you so much for your help!

                1 Reply Last reply
                0
                • J jsulm
                  14 May 2018, 06:40

                  @zzzhhhzzzhhh Is it possible that java outputs to stderr?

                  Z Offline
                  Z Offline
                  zzzhhhzzzhhh
                  wrote on 15 May 2018, 20:02 last edited by
                  #8

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

                  1 Reply Last reply
                  1

                  1/8

                  14 May 2018, 06:11

                  • Login

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