Search code examples
javasolaris

How to Use posix_spawn() in Java


I've inherited a legacy application that uses ProcessBuilder.start() to execute a script on a Solaris 10 server.

Unfortunately, this script call fails due to a memory issue, as documented here

Oracle's recommendation is to use posix_spawn() since, under the covers, ProcessBuilder.start() is using fork/exec.

I have been unable to find any examples (e.g., how to call "myScript.sh")

using posix_spawn() in Java, or even what are the packages that are required.

Could you please, point me to a simple example on how to use posix_spawn() in Java?


Solution

  • You will need to familiarize yourself with JNI first. Learn how to call out into a native routine from Java code. Once you do - you can look at this example and see if it helps with your issue. Of particular interest to you is:

    if( (RC=posix_spawn(&pid, spawnedArgs[0], NULL, NULL, spawnedArgs, NULL)) !=0 ){
        printf("Error while executing posix_spawn(), Return code from posix_spawn()=%d",RC);
    
    }