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. Qt not reading shell variables
Forum Updated to NodeBB v4.3 + New Features

Qt not reading shell variables

Scheduled Pinned Locked Moved Solved General and Desktop
qgetenvqprocessbashshell variables
7 Posts 3 Posters 5.0k Views 3 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.
  • J Offline
    J Offline
    Jan-Willem
    wrote on last edited by
    #1

    I've been working on this for some time, but can not find a suitable workaround.
    I have set JAVA_HOME and PATH on Linux. From the terminal everything works fine.
    "echo $JAVA_HOME" outputs the jdk-directory.

    From within QtCreator with qgetenv("JAVA_HOME") outputs nothing, same goes for QProcessEnvironment::systemEnvironment().

    When I start QtCreator or my Qt application from the command-line, it does see the JAVA_HOME.
    So I tried the following:

    QProcess sh;
    sh.start("bash --login");
    sh.write("source /home/jan-willem/.bashrc\n");
    sh.write("env");
    sh.closeWriteChannel();
    sh.waitForFinished();
    QByteArray output = sh.readAllStandardOutput();
    QByteArray error = sh.readAllStandardError();
    sh.close();
    qDebug() << output << error;
    

    It did not work. Only the system wide variables are shown.
    When I insert:

    sh.write("export JAVA_HOME=/opt/java/jdk1.8.0_121\n");
    

    it does work (obviously), but the idea is to check the environment variables to find the jdk-path, and not the other way around.

    Someone an idea?

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi!

      sh.start("bash --login");

      Are you sure this actually starts bash? Maybe try with "/bin/bash" and check the error() .

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

        Hi,

        To add to @Wieland, if you are looking for the environment variables of your process then QProcess::processEnvironment might be of interest.

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

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jan-Willem
          wrote on last edited by
          #4

          @Wieland and @SGaist

          I've modified the code as follows:

          QProcess sh;
          sh.start("/bin/bash"); // or /bin/bash --login,  bash or bash --login
          qDebug() << sh.error(); // QProcess::ProcessError(UnknownError)
          
          QProcessEnvironment pe = sh.processEnvironment();
          qDebug() << pe.value("PATH"); // returns nothing
          
          // sh.waitForFinished(); commented out because it freezes the GUI at this point
          
          sh.close();
          

          And

          QProcess sh;
          sh.setProcessEnvironment(QProcessEnvironment::systemEnvironment());
          sh.start("/bin/bash"); // or /bin/bash --login,  bash or bash --login
          qDebug() << sh.error(); // QProcess::ProcessError(UnknownError)
          
          QProcessEnvironment pe = sh.processEnvironment();
          qDebug() << pe.value("PATH"); // returns "/opt/Qt/5.8/gcc_64/bin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
          
          // sh.waitForFinished(); commented out because it freezes the GUI at this point
          
          sh.close();
          

          The .profile and .bashrc are for some reason not read. Perhaps it has something to do with the unknown error. I will try to find out what this error is.

          By the way, I noticed that the System Environment Variables in QtCreator also don't include the shell variables. I read somewhere that apps started from the desktop environment don't use bash or something to launch, which explains why QtCreator doesn't recognize the exported variables from .bashrc.

          But using a QProcess like above would normally be a good way to find those variables, am I right?

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

            What exactly do you want to do with these variables ?

            Depending on your needs qgetenv and its friends might be more suited.

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

            1 Reply Last reply
            0
            • J Offline
              J Offline
              Jan-Willem
              wrote on last edited by
              #6

              For my work I have to learn programming in Java. So as a fun-project (taking a break from studying) I created a code-editor using only Qt-examples and the Matching Parentheses example from the Qt Quarterly. I want to implement something like a first-run wizard which looks for a JAVA_HOME or ask the user to select the path to the JDK.

              qgetenv("PATH"); also just gives me "/opt/Qt/5.8/gcc_64/bin:/usr/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games". It was the first thing I tried ;-)

              As I said before, hen I start QtCreator or my Qt application from the command-line, it uses bash and therefore sees the JAVA_HOME variable.

              So, I could try find out what (gui-)shell launches QtCreator and other apps and perhaps see how ta export the shell variables to that shell also. But I thought starting bash form a QProcess should also work.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                Jan-Willem
                wrote on last edited by
                #7

                Well, actually it does work! But if bash is not running interactively, it won't read the .bashrc where the shell-variables are stored. So I did the following:

                sh.start("bash -i"); // the -i tell bash to run interactively
                if(sh.waitForStarted())
                    sh.write("printenv JAVA_HOME"); // or sh.write("echo $JAVA_HOME")
                
                sh.closeWriteChannel();
                

                In case you were wondering, I connected the QProcess::finished signal to a slot where the QProcess::readAllStandardOutput is put in a QPlainTextEdit.

                The QProcess::processEnvironment is not updated however. Even if I try to export JAVA_HOME with sh.write("export JAVA_HOME"). While that is not what I expected, I now can find the local shell-variables.

                So I will mark this problem as solved.
                Thanks!

                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