java.io.IOException: Error running exec(). Commands: [cd, sdcard/.yasmin] Working Directory: null Environment: null

前端 未结 4 1500
刺人心
刺人心 2021-02-06 07:40

I try to access to my folder in sdcard and install myapp.apk, i use this code:

 Runtime.getRuntime().exec(\"cd sdcard/.yasmin\");
 Runtime.getRuntime().exec(\"ad         


        
相关标签:
4条回答
  • 2021-02-06 07:45

    I am not sure that this will fix your problem, but AFAIK, each call to exec() creates a new shell. A possible solution is to do the following:

    1. Get the process of the exec() using: Process p = Runtime.getRuntime().exec(...).
    2. Grab the process inputStream using p.getInputStream();.
    3. Run the second command.

    also note that you are trying to access the sdcard as you were in root folder and in a hardcoded path, consider the following:

    Process p = Runtime.getRuntime().exec("cd /sdcard/.yasmin");
    

    Or even better:

    Process p = Runtime.getRuntime().exec("cd " + Environment.getExternalStorageDirectory() + "/.yasmin");
    

    Hope it'll help!

    0 讨论(0)
  • 2021-02-06 07:45

    You can get the file generated with log as follows:

    command - "your command" Environment - null Directory - null

    Runtime.getRuntime().exec("your command",null,null);

    0 讨论(0)
  • 2021-02-06 07:50
    1. Get the process of the exec() using: Process p = Runtime.getRuntime().exec(...).
    2. Grab the process inputStream using p.getInputStream();.
    3. Run the second command.

    I would have posted it as a comment to MByD's answer but I don't have 50 reputation yet so StackOverflow won't let me.

    For the second step you are supposed to use DataOutputStream instead of DataInputStream to write commands to the shell. InsputDataStream is for the opposite purpose in fact that is to read output.

    If it sounds confusing then think of it this way: You are outputting the commands through the Android Debug Bridge (ADB) to the shell hence using DataOutputStream. On the other hand you are getting result through the ADB from the shell hence using DataInputStream.

    0 讨论(0)
  • 2021-02-06 07:51

    you should use Runtime.getRuntime().exec("sh -c cd /sdcard/.yasmin");

    0 讨论(0)
提交回复
热议问题