[英]unable to compile and run c code through process builder in android
I am trying to comile and run c code using process builder but it fail on UNIXProcess native method can not run gcc permission denied,what permission is it talking about ? 我正在尝试使用进程生成器编译和运行C代码,但在UNIX上失败,它无法运行gcc许可,这是在说什么许可? please help i am not able to understand problem 请帮助我无法理解问题
class MainActivity : AppCompatActivity() {
val CMD_COMPILE_C_CODE ="gcc -o"
val cFileName= "hello.c"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val file = File(Environment.getExternalStorageDirectory().absolutePath + File.separator + cFileName)
file.createNewFile()
file.setExecutable(true)
//write the bytes in file
if (file.exists()) {
val fo = FileOutputStream(file)
val osw = OutputStreamWriter(fo)
osw.write("#include <stdio.h>\n" +
"int main(void)\n" +
"{\n" +
"printf(\"Hello World\\n\");\n" +
"return 0;\n" +
"}\n")
fo.close()
Log.i("Vikas","file created: $file")
}
val cFilePath = Environment.getExternalStorageDirectory().absolutePath + File.separator + cFileName
excCFile(cFilePath)
}
private fun excCFile(fileName: String) {
val className = fileName.split(".").get(0)
val listOfCmd = listOf<String>("$CMD_COMPILE_C_CODE $fileName")
val pb = ProcessBuilder("gcc", "$fileName")
result.setText(pb.start().inputStream.read())
}
}
but getting error :: 但出现错误::
Caused by: java.io.IOException: Cannot run program "gcc": error=13, Permission denied
at java.lang.ProcessBuilder.start(ProcessBuilder.java:983)
at example.androidtest.MainActivity.excCFile(MainActivity.kt:49)
at example.androidtest.MainActivity.onCreate(MainActivity.kt:41)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.io.IOException: error=13, Permission denied
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:133)
也许您需要以root用户(管理员)身份运行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.