Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-can';t在模拟器中执行简单cd_Java_Android_Shell_Command_Emulation - Fatal编程技术网

Java Android-can';t在模拟器中执行简单cd

Java Android-can';t在模拟器中执行简单cd,java,android,shell,command,emulation,Java,Android,Shell,Command,Emulation,我有一个仿真器,我根(虽然我不确定它是否正确) 相关的) 我正在尝试为我的论文构建一个根应用程序,当一个按钮 按下按钮,它将尝试查找数据库文件 我已经达到了尝试从中执行shell命令的程度 在应用程序中(java代码),但我所能做的就是ls 当我尝试将cd放入另一个文件夹时,它不起作用。在cd之后 我执行pwd或ls,它们仍然给出/的结果 下面是我代码的相关部分 Button btn_read = (Button) findViewById(R.id.button); btn_read.

我有一个仿真器,我根(虽然我不确定它是否正确) 相关的)

  • 我正在尝试为我的论文构建一个根应用程序,当一个按钮 按下按钮,它将尝试查找数据库文件

  • 我已经达到了尝试从中执行shell命令的程度 在应用程序中(java代码),但我所能做的就是
    ls

  • 当我尝试将
    cd
    放入另一个文件夹时,它不起作用。在
    cd之后
    我执行
    pwd
    ls
    ,它们仍然给出
    /
    的结果

  • 下面是我代码的相关部分

    Button btn_read = (Button) findViewById(R.id.button);
        btn_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*String s;
    
                try {
                    Process p = Runtime.getRuntime().exec("ls -al");
                    p = Runtime.getRuntime().exec("adb shell");
                    p = Runtime.getRuntime().exec("cd data/data");
    
                    BufferedReader stdInput = new BufferedReader(new
                            InputStreamReader(p.getInputStream()));
    
                    BufferedReader stdError = new BufferedReader(new
                            InputStreamReader(p.getErrorStream()));
    
                    // read the output from the command
                    Log.d("ReadDB", "Here is the standard output of the command:\n");
                    while ((s = stdInput.readLine()) != null) {
                        Log.d("ReadDB", s);
                    }
    
                    // read any errors from the attempted command
                    Log.d("ReadDB", "Here is the standard error of the command (if any):\n");
                    while ((s = stdError.readLine()) != null) {
                        Log.d("ReadDB", s);
                    }
    
                    System.exit(0);
                }
                catch (IOException e) {
                    Log.d("ReadDB", "Exception happened - here's what I know: ");
                    e.printStackTrace();
                    System.exit(-1);
                }*/
    
                executeCommand(new String[] {
                    "sh", "-c", "ls -l"
                });
                /*executeCommand(new String[] {
                    "sh", "-c", "cd /mnt"
                });*/
                executeCommand(new String[] {
                    "sh", "-c", "ls -l data/data"
                });
                executeCommand(new String[] {
                    "sh", "-c", "pwd"
                });
            }
        });
    }
    
    private static void executeCommand(String[] commands) {
        Process p;
        try {
            p = Runtime.getRuntime().exec(commands);
            p.waitFor();
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(p.getInputStream()));
    
            String line = "";
            while ((line = reader.readLine())!= null) {
                Log.d("ReadDB", line + "\n");
            }
            Log.d("ReadDB", "--------------------------------------------------------------------------------------");
    
        } catch (Exception e) {
            Log.d("ReadDB", "Exception occured");
            e.printStackTrace();
        }
    }
    

  • 有人能帮忙吗?

    这些命令将单独运行,因此这是不可能的。什么应该起作用:


    谢谢它似乎起作用了
    executeCommand(new String[] {
        "sh", "-c", "cd /mnt && some other command"
    });