Question

I'm in the process of writing a compiled C application which uses the system() function to launch a Java .jar file:

int main() {
  system("java -jar MyJar.jar");
  return 0;
}

I successfully wrapped this up in a clickable app bundle, however, when I double click it, the application exits immediately before it has a chance to launch the jar. However it works perfectly when I run the compiled C code from the command line.

Any insight would be appreciated!

Scott

Était-ce utile?

La solution

The reason the application exits immediately is because of the following line:

return 0;

You would want to use exec instead of system. With exec, your program gets replaced by the Java process and never gets a chance to reach the return 0; line. However, it's much easier to just replace the entire C progrma with a shell script:

#!/bin/sh
exec java -jar MyJar.jar

As written, there is no drawback to this approach that I can think of. The C program already spawns a shell process (that's what system does), so why not start out with a shell process in the first place?

Lots of application bundles use shell scripts to do things like this.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top