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. How can I execute a jar file in resources of the project with QProcess?
Forum Updated to NodeBB v4.3 + New Features

How can I execute a jar file in resources of the project with QProcess?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 676 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.
  • atreidexA Offline
    atreidexA Offline
    atreidex
    wrote on last edited by
    #1

    I can execute a jar file when I give the full path of the file to the function with the command below:

    process->start("~/Documents/program.jar");
    

    But I don't know how to do it when I add the jar file into resources of the project. I tried to use "qrc://.." or ":/..." paths of the resource file but it didn't work. I think I should tweak the .pro file but I couldn't manage it.

    JonBJ 1 Reply Last reply
    0
    • atreidexA atreidex

      I can execute a jar file when I give the full path of the file to the function with the command below:

      process->start("~/Documents/program.jar");
      

      But I don't know how to do it when I add the jar file into resources of the project. I tried to use "qrc://.." or ":/..." paths of the resource file but it didn't work. I think I should tweak the .pro file but I couldn't manage it.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @atreidex
      You won't be able to execute a process on a file inside your resources, because it's not an actual external file.

      You'll need to use QFile to read the contents of that "pseudo-file", writing it to a real, physical, external temporary file with the same name. Then do your process against that file, and delete it when you're done.

      1 Reply Last reply
      4
      • atreidexA Offline
        atreidexA Offline
        atreidex
        wrote on last edited by
        #3

        Thank you, I did what you said and it worked! Here is my code if anyone interested:

        QTemporaryDir tempDir; // Must be in the ".h" file.
        QString outputPath;
        
        ...
        
        if (tempDir.isValid())
        {
            outputPath = tempDir.path() + "/Program.jar";
            // ":/Files/Program.jar" is the path of the resource file:
            QFile::copy(":/Files/Program.jar",  outputPath)
        }
        
        ...
        
        // Starting the process:
        process->start(outputPath);
        
        JonBJ 1 Reply Last reply
        0
        • atreidexA atreidex

          Thank you, I did what you said and it worked! Here is my code if anyone interested:

          QTemporaryDir tempDir; // Must be in the ".h" file.
          QString outputPath;
          
          ...
          
          if (tempDir.isValid())
          {
              outputPath = tempDir.path() + "/Program.jar";
              // ":/Files/Program.jar" is the path of the resource file:
              QFile::copy(":/Files/Program.jar",  outputPath)
          }
          
          ...
          
          // Starting the process:
          process->start(outputPath);
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @atreidex
          Dangerous, probably/possibly! QTemporaryDir:

          and the directory will subsequently be removed upon destruction of the QTemporaryDir object.

          If you let your QTemporaryDir tempDir variable go out of scope while the process has been started but not yet finished, it will attempt to delete the directory containing the extracted .jar file at that instant. Depending on your OS/which way the wind is blowing this might:

          • Error when it tries to delete (hopefully not).
          • Cause grief to the process running on/from the .jar file.
          • Silently fail deleting the directory and never delete it.
          • Work without problem :)

          Think about the scope of your QTemporaryDir/timing of deletion compared to when the process finishes.

          Also you're supposed to test tempDir.isValid(), if you're not doing so. Whoops, you are, sorry.

          Because this might behave funnily at run-time for your users and they/you may not know, you should code to check/report all errors in this code.

          atreidexA 1 Reply Last reply
          3
          • JonBJ JonB

            @atreidex
            Dangerous, probably/possibly! QTemporaryDir:

            and the directory will subsequently be removed upon destruction of the QTemporaryDir object.

            If you let your QTemporaryDir tempDir variable go out of scope while the process has been started but not yet finished, it will attempt to delete the directory containing the extracted .jar file at that instant. Depending on your OS/which way the wind is blowing this might:

            • Error when it tries to delete (hopefully not).
            • Cause grief to the process running on/from the .jar file.
            • Silently fail deleting the directory and never delete it.
            • Work without problem :)

            Think about the scope of your QTemporaryDir/timing of deletion compared to when the process finishes.

            Also you're supposed to test tempDir.isValid(), if you're not doing so. Whoops, you are, sorry.

            Because this might behave funnily at run-time for your users and they/you may not know, you should code to check/report all errors in this code.

            atreidexA Offline
            atreidexA Offline
            atreidex
            wrote on last edited by
            #5

            @JonB Thanks for clarification!

            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