简体   繁体   中英

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 ? 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用户(管理员)身份运行它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM