在PC上调用cmd,进行一些常用的命令操作,在Android上的是通过Runtime.getRuntime().exec来执行底层Linux下的程序或脚本(bat)。
首先连接上真机,电脑打开CMD,输入adb-shell,确保你要进行的脚本语言是可以执行的。(比如常见的ping命令)
但是深入一下,发现使用ping命令,并附加一些参数,我们设置 -w 5 了,希望5秒钟如果没有ping通,可以有返回,可是像如下常规的操作,貌似ping下的附加参数是不会起作用的,也就是说在cmd下,我希望ping后5秒没有收到包就返回,但是在android下执行就不会有效果:
Process process = null;
InputStream instream = null;
BufferedReader bufferReader = null;
try {
process = Runtime.getRuntime().exec(command);
instream = process.getInputStream();
bufferReader = new BufferedReader(new InputStreamReader(instream, "GBK"));
String readline;
while ((readline = bufferReader.readLine()) != null) {
results.add(readline);
// Log.i(TAG, "execute command result : " + readline);
int status = process.waitFor();
Log.i(TAG, "execute command :" + command + ", status : " + status);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (InterruptedException e) {
Log.e(TAG, e.getMessage());
这种常规的操作,如果执行一条Ping命令,当在某个
ip下卡住ping不通时,就有问题了,会发现代码一直会阻塞在
br.readLine()的地方,任何办法都不好解决,网上说的把操作放在另外一个Thread里进行,只是解决了process.waiFor()的阻塞问题。其实也解决不了当进行ping不通时readline阻塞的问题,在ping命令的操作下使用readline并不像读取一个文件,当遇到换行时会结束,ping不通,只能一直阻塞着,除非在外部进行close等操作。
结合国内外论坛,终于找到一个办法,我写成了一个方法类,供大家参考(可直接调用):
package com.vixtel.netvista.gdcmcc.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.util.Log;
* 执行命令行工具类
* @author yangxiaolong 2014-04-30
public class CommandUtil {
public static final String TAG = CommandUtil.class.getSimpleName();
public static final String COMMAND_SH = "sh";
public static final String COMMAND_LINE_END = "\n";
public static final String COMMAND_EXIT = "exit\n";
private static final boolean ISDEBUG = true;
* 执行单条命令
* @param command
* @return
public static List<String> execute(String command) {
return execute(new String[] { command });
* 可执行多行命令(bat)
* @param commands
* @return
public static List<String> execute(String[] commands) {
List<String> results = new ArrayList<String>();
int status = -1;
if (commands == null || commands.length == 0) {
return null;
debug("execute command start : " + commands);
Process process = null;
BufferedReader successReader = null;
BufferedReader errorReader = null;
StringBuilder errorMsg = null;
DataOutputStream dos = null;
try {
// TODO
process = Runtime.getRuntime().exec(COMMAND_SH);
dos = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) {
continue;
dos.write(command.getBytes());
dos.writeBytes(COMMAND_LINE_END);
dos.flush();
dos.writeBytes(COMMAND_EXIT);
dos.flush();
status = process.waitFor();
errorMsg = new StringBuilder();
successReader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
errorReader = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
String lineStr;
while ((lineStr = successReader.readLine()) != null) {
results.add(lineStr);
debug(" command line item : " + lineStr);
while ((lineStr = errorReader.readLine()) != null) {
errorMsg.append(lineStr);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dos != null) {
dos.close();
if (successReader != null) {
successReader.close();
if (errorReader != null) {
errorReader.close();
} catch (IOException e) {
e.printStackTrace();
if (process != null) {
process.destroy();
debug(String.format(Locale.CHINA,
"execute command end,errorMsg:%s,and status %d: ", errorMsg,
status));
return results;
* DEBUG LOG
* @param message
private static void debug(String message) {
if (ISDEBUG) {
Log.d(TAG, message);
windows环境下IDEA java代码Runtime.getRuntime.exec中shell的执行环境的解决方案前言解决办法后记
在使用IDEA本地开发监控守护线程的后台,我遇上了执行环境不兼容的问题,爆出各种“xxx不是内部或外部命令,也不是可运行的程序或批处理文件。”,简而言之就是Windows下的CMD默认不识别shell环境下的诸如sh、ssh、cat等命令。这个小demo很小,但是因为公司架构要求前后端分离+docker容器部署,所以这其中的坑还是不少的,执行环境不兼容就是其中额问题之一。
解决办法
下载Git Bash进行安装,安装Git Bash是为了方便我们能在w
Runtime.getRuntime().exec 用于调用外部可执行程序或系统命令,并重定向外部程序的标准输入、标准输出和标准错误到缓冲池。功能和windows“运行”类似。
一:问题描述
在已经root过的android设备下,app执行一个linux命令,app需要获取su权限,在某些android主板下会出现异常, Command: [su] Working Directory: null Environment: null,代码如下:
private void execLinuxCommand(String cmd){
Runtime runtime = Runtime.getRuntime();
try {
Process localProcess = runtime.exec(su);
OutputStream loca
MayI是另一个库,它简化了运行Android Marshmallow及更高版本的设备在运行时请求权限的过程。
从Android Marshmallow及更高版本开始,添加了一项新功能,该功能使用户Gran MayI成为另一个库,该库简化了运行Android Marshmallow及更高版本的设备在运行时请求权限的过程。
从Android棉花糖开始,添加了一项新功能,该功能使用户可以在应用运行时授予或拒绝权限,而不是在安装应用时将其全部授予。
这种方法使用户可以更好地控制应用程序,但需要开发人员添加大量代码来支持它。
该库旨在减少样板
getRuntimeRuntime.getRuntime().exec共有六个重载方法:public Process exec(String command)public Process exec(String [] cmdArray)public Process exec(String command, String [] envp)public Process exec(String [] cmdArray, String [] envp)public Process exec(String comma
在Android上调用Runtime.getRuntime().exec()函数执行命令时,报错了。
java.io.IOException: Cannot run program "/data/data/com.example.testhband/outTest": error=13, Permission denied
使用命令 run-as com.example.testhband进入APK安装目录,发现outTest存在,权限也是正常的。
再查看应用程序的AndroidManifest.xm
public static String execute_command(String cmd) {
try {
//String keyCommand = "setprop " + propName;
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
public String execRootCmd(String cmd) {String result = "";DataOutputStream dos = null;DataInputStream dis = null;try {Process p = Runtime.getRuntime().exec("su");// 经过Root处理的android系统即有su命令dos = new D...
Android Runtime使得直接调用底层Linux下的可执行程序或脚本成为可能
比如Linux下写个测试工具,直接编译后apk中通过Runtime来调用
或者写个脚本,apk中直接调用,省去中间层或者JNI
这个至少效率应该比较高吧
[java]
view plaincopy
public class test extends Activi
Runtime.getRuntime().exec执行命令的问题
平常写android代码时,经常使用Runtime.getRuntime().exec来执行一段linux命令,
如果是一些简单的命令可以直接使用:
Runtime.getRuntime().exec(cmd); //cmd一个字符串
可以正常执行,但是如果是一些相对复杂一点点的比如:
cat apk路径 | pm install -S apk大小
会一直报cat: Unknown option S (see “cat --help”)
Runtime.getRuntime()这个问题在android中申请root权限时一定碰到,它究竟是一个怎样的api呢?
文章http://blog.csdn.net/xubo578/article/details/5717186文章给出了一定的理解.