How to compile Java Code got from editText in android studio

How about saving user’s code somewhere in the device as .java file and then running shell command for compiling file and processing user’s code.
refere below code

try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
outputStream.flush();

outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
    throw new Exception(e);
}catch(InterruptedException e){
    throw new Exception(e);
}

Not providing required shell commands for the purpose as they would vary language to language but easy to explore.

Other option could be to call some online service that can do the compilation and provide the result.

Leave a Comment