Menu

#74 Avoid Runtime.getRuntime().exec : JNA ?

open
nobody
5
2012-01-23
2012-01-23
No

This method cause a fork of the JVM and the fork duplicates memory.
So instead of giving 4Go to your application server which use JLine, you have to limit the JVM memory at 2Go.

IMHO the best choice is to use JNA to call system(char *) method.
JNA is a small (1Mo) library which performs "reflexion" on C. Its code use JNI and is compiled on a lot of platform.

Java Code of Runtime.exec substitution
-------------

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;

public class Runtime {

private interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
int system(String cmd);
}

public static int exec(String command) {
return CLibrary.INSTANCE.system(command);
}

}

Discussion


Log in to post a comment.