66import java .io .*;
77import java .util .*;
88import java .util .concurrent .*;
9+ import android .content .Context ;
10+ import android .app .Activity ;
911
1012public class Executor extends CordovaPlugin {
1113
1214 private final Map <String , Process > processes = new ConcurrentHashMap <>();
1315 private final Map <String , OutputStream > processInputs = new ConcurrentHashMap <>();
1416 private final Map <String , CallbackContext > processCallbacks = new ConcurrentHashMap <>();
1517
18+ private Context context ;
19+
20+
21+ @ Override
22+ public void initialize (CordovaInterface cordova , CordovaWebView webView ) {
23+ super .initialize (cordova , webView );
24+ this .context = cordova .getContext ();
25+
26+ }
27+
28+
29+
30+
1631 @ Override
1732 public boolean execute (String action , JSONArray args , CallbackContext callbackContext ) throws JSONException {
1833 switch (action ) {
@@ -35,8 +50,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
3550 exec (cmdExec , callbackContext );
3651 return true ;
3752 case "isRunning" :
38- String pid = args .getString (0 );
39- isProcessRunning (pid , callbackContext );
53+ isProcessRunning (args .getString (0 ), callbackContext );
4054 return true ;
4155 default :
4256 callbackContext .error ("Unknown action: " + action );
@@ -47,7 +61,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo
4761 private void exec (String cmd , CallbackContext callbackContext ) {
4862 try {
4963 if (cmd != null && !cmd .isEmpty ()) {
50- Process process = Runtime .getRuntime ().exec (cmd );
64+ ProcessBuilder builder = new ProcessBuilder ("sh" , "-c" , cmd );
65+
66+ // Set environment variables
67+ Map <String , String > env = builder .environment ();
68+ env .put ("PREFIX" , context .getFilesDir ().getAbsolutePath ());
69+ env .put ("NATIVE_DIR" , context .getApplicationInfo ().nativeLibraryDir );
70+
71+ Process process = builder .start ();
5172
5273 // Capture stdout
5374 BufferedReader stdOutReader = new BufferedReader (
@@ -88,7 +109,15 @@ private void exec(String cmd, CallbackContext callbackContext) {
88109 private void startProcess (String pid , String cmd , CallbackContext callbackContext ) {
89110 cordova .getThreadPool ().execute (() -> {
90111 try {
91- Process process = Runtime .getRuntime ().exec (cmd );
112+ ProcessBuilder builder = new ProcessBuilder ("sh" , "-c" , cmd );
113+
114+ // Set environment variables
115+ Map <String , String > env = builder .environment ();
116+ env .put ("PREFIX" , context .getFilesDir ().getAbsolutePath ());
117+ env .put ("NATIVE_DIR" , context .getApplicationInfo ().nativeLibraryDir );
118+
119+ Process process = builder .start ();
120+
92121 processes .put (pid , process );
93122 processInputs .put (pid , process .getOutputStream ());
94123 processCallbacks .put (pid , callbackContext );
0 commit comments