问题描述:

i am trying this code

public String ReadFromCMD()

{

ProcessBuilder cmd;

String result="";

//int resultshow = 0;

try{

String[] args = {"/system/bin/top -m 10 -n 1"};

cmd = new ProcessBuilder(strCmdArgs);

Process process = cmd.start();

InputStream in = process.getInputStream();

BufferedReader reader = new BufferedReader(

new InputStreamReader(process.getInputStream()));

int read;

char[] buffer = new char[4096];

StringBuffer output = new StringBuffer();

process.waitFor();

while ((read = reader.read(buffer)) > 0) {

output.append(buffer, 0, read);

}

reader.close();

in.close();

} catch(Exception ex){

ex.printStackTrace();

}

return result;

}

Error comes :

03-25 09:57:36.885: W/System.err(30300): java.io.IOException: Error running exec().

Command: [/system/bin/top -m 10 -n 1] Working Directory: null Environment:

[ANDROID_SOCKET_zygote=10, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage

/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF,

ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/storage/sdcard0, ANDROID_ASSETS=/system/app,

PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ASEC_MOUNTPOINT=/mnt/asec,

LOOP_MOUNTPOINT=/mnt/obb, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-

junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework

/framework.jar:/system/framework/framework2.jar:/system/framework/android.policy.jar:

/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework

/sec_edm.jar:/system/framework/seccamera.jar, ANDROID_DATA=/data, LD_LIBRARY_PATH=/vendor

/lib:/system/lib, ANDROID_ROOT=/system, ANDROID_PROPERTY_WORKSPACE=9,66560,

VIBE_PIPE_PATH=/dev/pipes]

03-25 09:57:36.885: W/System.err(30300): at java.lang.ProcessManager.exec(ProcessManager.java:211)

03-25 09:57:36.885: W/System.err(30300): at java.lang.ProcessBuilder.start(ProcessBuilder.java:195)

This error comes at line

Process process = cmd.start();

Please help me to solve this problem

网友答案:

This:

String[] args = {"/system/bin/top -m 10 -n 1"};

means that your executable is a file named top -m 10 -n 1 in the directory /system/bin.

What you want is an executable named top in that directory, with -m 10 -n 1 as four separate arguments:

String[] args = {"/system/bin/top", "-m", "10", "-n", "1"};

Alternatively, since none of those components contains a space, you could "cheat" a bit and write something like:

String[] args = "/system/bin/top -m 10 -n 1".split(" ");

which is the same (it still creates an array of five strings), but is arguably a bit easier to read.

相关阅读:
Top