skip to Main Content

I am making an android app which can run c, c++ and java programs. The app stores the respective files in a folder and is made to execute with the following code. Whenever I click on compile button it shows an IO Exception saying "error=13 permission denied".

 try {
                p = Runtime.getRuntime().exec(path + "/PocketIDE/JavaPrograms/"+ filename);
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(p.getInputStream()));
                String line = "";
                while ((line = reader.readLine()) != null) {
                    output2.append(line).append("n");
                    p.waitFor();
                }
                String response = output2.toString();
                output.setText(response);
            } catch (IOException | InterruptedException e) {
                output.setText(e.toString());
                e.printStackTrace();
            }

Is the above method the correct way to execute the program? or do I need to change the code?

2

Answers


  1.             p = Runtime.getRuntime().exec(path + "/PocketIDE/JavaPrograms/"+ filename);
    

    You shouldn’t run arbitrary Java code with the runtime that controls the execution of your app. This opens a massive security flaw, so Android disallows it. Instead, you should find a way to execute the Java code in its own environment and runtime.

    Login or Signup to reply.
  2. The statement in your code can be used to execute other programs, but it is not necessarily a good idea.

    The exec method internally forks the application`s process and creates a new one, which immediately executes the system command you give it.

    From the path in your code I assume that you try to execute a binary executable, which is not allowed anymore by Android since API level 28:

    Untrusted apps that target Android 10 cannot invoke exec() on files within the app’s home directory. This execution of files from the writable app home directory is a W^X violation. Apps should load only the binary code that’s embedded within an app’s APK file.

    The only possible solution is to reduce the API level to 28 or include the binary in the APK file during packaging.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search