Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Use posix_spawn() in Java

Tags:

java

solaris

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?

like image 680
aparcel Avatar asked Feb 05 '26 02:02

aparcel


2 Answers

Recent version of Java 7 and 8 support posix_spawn internally.

command line option

-Djdk.lang.Process.launchMechanism=POSIX_SPAWN

or enable at runtime

System.setProperty("jdk.lang.Process.launchMechanism", "POSIX_SPAWN");

I'm a little confused as to which Java version/OS combinations have this enabled by default, but I'm sure you could test and find out pretty quickly whether setting this option makes a difference.

For reference, to go back to the old fork method simply use

-Djdk.lang.Process.launchMechanism=fork

To prove whether this option is respected in your JVM version use

-Djdk.lang.Process.launchMechanism=dummy

and you will get an error next time you exec. This way you know the JVM is receiving this option.

like image 92
700 Software Avatar answered Feb 06 '26 16:02

700 Software


An alternative, which does not require JNI, is to create a separate "process spawner" application. I would probably have this application expose an RMI interface, and create a wrapper object that is a drop-in replacement for ProcessBuilder.

You might also want to consider having this "spawner" application be the thing that starts your legacy application.

like image 25
Anon Avatar answered Feb 06 '26 17:02

Anon