You'd have to do this in two steps:
Extract the binary and write it to disk (because the OS is probably only able to launch the program if it's a file on disk)
Launch the program, preferably using ProcessBuilder
.
Here's a complete demo:
test.c
#include <stdio.h>
int main() {
printf("Hello world\n");
return 0;
}
Test.java
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Collections;
public class Test {
public static void main(String[] args) throws Exception {
Path binary = Paths.get("mybinary");
Files.copy(Test.class.getResourceAsStream("mybinary"), binary);
System.out.println("Launching external program...");
// Needed for Linux at least
Files.setPosixFilePermissions(binary,
Collections.singleton(PosixFilePermission.OWNER_EXECUTE));
Process p = new ProcessBuilder("./mybinary").inheritIO().start();
p.waitFor();
System.out.println("External program finished.");
}
}
Demo
$ cc mybinary.c -o mybinary
$ ./mybinary
Hello world
$ javac Test.java
$ jar -cvf Test.jar Test.class mybinary
$ rm mybinary Test.class
$ ls
mybinary.c Test.jar Test.java
$ java -cp Test.jar Test
Launching external program...
Hello world
External program finished.
Further reading: