zoukankan      html  css  js  c++  java
  • java执行shell命令,chmod 777 xxx,改变权限无效的解决办法。

    在java程序中执行shell命令,改变文件的权限,可以在命令行中执行
    chmod 777 <span style="font-family: Arial, Helvetica, sans-serif;">/data/misc/123.sh"</span>
    来改变权限,但是在java代码中执行这个命令时使用
    
    Runtime.getRuntime().exec("chmod 777 /data/misc/123.sh");
    无效,使用
    String[] command = new String[] {"/system/bin/sh","-c","chmod 777 /data/misc/123.sh"};
    Runtime.getRuntime().exec(command);
    
    同样无效
    最后通过实例化一个dataoutputstream对象,在这个对象的写字节方法里写命令才实现,代码如下:
    
    String[] commands = new String[] { "/system/bin/sh", "-c",
                "chmod -R 777 /data/misc/123.sh" };
        Process process = null;
        DataOutputStream dataOutputStream = null;
    try {
    
                process = Runtime.getRuntime().exec("su");
                dataOutputStream = new DataOutputStream(process.getOutputStream());
                int length = commands.length;
                for (int i = 0; i < length; i++) {
                    dataOutputStream.writeBytes(commands[i] + "
    ");
                }
                dataOutputStream.writeBytes("exit
    ");
                dataOutputStream.flush();
                process.waitFor();
            } catch (Exception e) {
    
            } finally {
                try {
                    if (dataOutputStream != null) {
                        dataOutputStream.close();
                    }
                    process.destroy();
                } catch (Exception e) {
                }
            }
  • 相关阅读:
    vuejs开发环境搭建
    贝塞尔曲线(cubic bezier)
    解决安装mysql的”A Windows service with the name MySQL already exists.“问题
    display:inline-block的间隙问题和解决办法
    限制两行显示,超出部分省略号
    border-radius四个值的问题
    PHP环境搭建
    CSS3属性box-sizing
    -webkit-tap-highlight-color
    gdb命令
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317312.html
Copyright © 2011-2022 走看看