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
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.
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:
The only possible solution is to reduce the API level to 28 or include the binary in the APK file during packaging.