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) {
    			}
    		}





  • 相关阅读:
    第二章、开发环境部署
    第一章、数据分析介绍
    爬虫之Beautiful Soup
    爬虫之selenium
    使用Python连接Mongodb,对数据库进行操作
    Python练习实例002
    Python练习实例001
    Python入门练手100例
    Python
    剑指Offer-003:从尾到头打印链表
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6783407.html
Copyright © 2011-2022 走看看