Search code examples
c++linuxexecl

How to execute a program in C++ Linux


I have a cpp project, which executes another program.
Here is my test:

int main() {
    execl("java -jar /pathOfJAR/myjar.jar", NULL);

    return 0;
}

I ran this project and I got nothing.

Then I tried like this:

execl("java", "-jar", "/pathOfJAR/myjar.jar");

I got an error:

Error: Could not find or load main class .pathOfJAR.myjar.jar

However, I can run the command in the terminal:

java -jar /pathOfJAR/myjar.jar

and I can get the right result.

How to use the function execl or I used the wrong function?


Solution

  • Try:

    execl("/bin/java", "java", "-jar", "/pathOfJAR/myjar.jar", NULL);
    

    Note that "/bin/java" should be replaced with the full path to your java interpreter, most easily determined with which java.