How can I execute a jar file in resources of the project with QProcess?
-
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.
-
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.
@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. -
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);
-
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);
@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 testWhoops, you are, sorry.tempDir.isValid()
, if you're not doing so.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.
-
@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 testWhoops, you are, sorry.tempDir.isValid()
, if you're not doing so.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.