正在使用运行时 class 执行 'adb logcat' 命令
Executing 'adb logcat' command using Runtime class
我试图将 logcat 内容放入 JTextPane 中。我使用了以下代码希望它将 return 内容作为 String 但它冻结并且不会产生错误。
Process exec = null;
try {
exec = Runtime.getRuntime().exec("adb logcat -d");
InputStream errorStream = exec.getErrorStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(errorStream));
String errorLine;
while ((errorLine = ebr.readLine()) != null) {
System.out.println("[ERROR] :- " + errorLine);
}
if (exec.waitFor() == 0) {
InputStream infoStream = exec.getInputStream();
InputStreamReader isr = new InputStreamReader(infoStream);
BufferedReader ibr = new BufferedReader(isr);
String infoLine;
while ((infoLine = ibr.readLine()) != null) {
System.out.println("[INFO] :- " + infoLine);
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
} finally {
if (exec != null) {
exec.destroy();
}
}
我参考了一些教程,但它们没有解决我的问题。这是错误的吗?是否有任何其他方法以编程方式将 logcat 内容作为字符串获取?对不起,如果这是一个愚蠢的问题。
您遇到的问题是您正在尝试处理命令流并等待执行进程,所有这些都在同一个线程中。它是阻塞的,因为读取流的进程正在等待进程并且您正在丢失流输入。
您要做的是在另一个线程中实现 reads/processes 命令输出(输入流)的功能,并在启动进程时启动该线程。
其次,您可能希望使用 ProcessBuilder
而不是 Runtime.exec
。
像这样的东西可以适应做你想做的事:
public class Test {
public static void main(String[] args) throws Exception {
String startDir = System.getProperty("user.dir"); // start in current dir (change if needed)
ProcessBuilder pb = new ProcessBuilder("adb","logcat","-d");
pb.directory(new File(startDir)); // start directory
pb.redirectErrorStream(true); // redirect the error stream to stdout
Process p = pb.start(); // start the process
// start a new thread to handle the stream input
new Thread(new ProcessTestRunnable(p)).start();
p.waitFor(); // wait if needed
}
// mimics stream gobbler, but allows user to process the result
static class ProcessTestRunnable implements Runnable {
Process p;
BufferedReader br;
ProcessTestRunnable(Process p) {
this.p = p;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(p.getInputStream());
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
// do something with the output here...
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
我试图将 logcat 内容放入 JTextPane 中。我使用了以下代码希望它将 return 内容作为 String 但它冻结并且不会产生错误。
Process exec = null;
try {
exec = Runtime.getRuntime().exec("adb logcat -d");
InputStream errorStream = exec.getErrorStream();
BufferedReader ebr = new BufferedReader(new InputStreamReader(errorStream));
String errorLine;
while ((errorLine = ebr.readLine()) != null) {
System.out.println("[ERROR] :- " + errorLine);
}
if (exec.waitFor() == 0) {
InputStream infoStream = exec.getInputStream();
InputStreamReader isr = new InputStreamReader(infoStream);
BufferedReader ibr = new BufferedReader(isr);
String infoLine;
while ((infoLine = ibr.readLine()) != null) {
System.out.println("[INFO] :- " + infoLine);
}
}
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
} finally {
if (exec != null) {
exec.destroy();
}
}
我参考了一些教程,但它们没有解决我的问题。这是错误的吗?是否有任何其他方法以编程方式将 logcat 内容作为字符串获取?对不起,如果这是一个愚蠢的问题。
您遇到的问题是您正在尝试处理命令流并等待执行进程,所有这些都在同一个线程中。它是阻塞的,因为读取流的进程正在等待进程并且您正在丢失流输入。
您要做的是在另一个线程中实现 reads/processes 命令输出(输入流)的功能,并在启动进程时启动该线程。
其次,您可能希望使用 ProcessBuilder
而不是 Runtime.exec
。
像这样的东西可以适应做你想做的事:
public class Test {
public static void main(String[] args) throws Exception {
String startDir = System.getProperty("user.dir"); // start in current dir (change if needed)
ProcessBuilder pb = new ProcessBuilder("adb","logcat","-d");
pb.directory(new File(startDir)); // start directory
pb.redirectErrorStream(true); // redirect the error stream to stdout
Process p = pb.start(); // start the process
// start a new thread to handle the stream input
new Thread(new ProcessTestRunnable(p)).start();
p.waitFor(); // wait if needed
}
// mimics stream gobbler, but allows user to process the result
static class ProcessTestRunnable implements Runnable {
Process p;
BufferedReader br;
ProcessTestRunnable(Process p) {
this.p = p;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(p.getInputStream());
br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
{
// do something with the output here...
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}