[英]Read Error when using Runtime.exec
This question has been asked once before on Stack Overflow , but the stack trace is different than mine, and I don't think it is caused by the same thing. 之前在Stack Overflow上曾问过这个问题,但是堆栈跟踪与我的不同,我认为这不是同一件事引起的。
This is my stack trace: 这是我的堆栈跟踪:
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at General.App.cmdString(App.java:393)
at General.App.cpauString(App.java:422)
at Functionality.RegistryScanThread.USBDScan(RegistryScanThread.java:109)
at Functionality.RegistryScanThread.doInBackground(RegistryScanThread.java:51)
at Functionality.RegistryScanThread.doInBackground(RegistryScanThread.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
This is a simplified version of the more complex program I wrote: 这是我编写的更复杂程序的简化版本:
import java.util.*;
import java.io.*;
public class Test {
//global vars
public Test(){
// initializeing stuffs
}
public static void main(String[] args){
ArrayList<String> str = cmdString("reg query hklm");
for(String s : str){
System.out.println(s);
}
}
public static ArrayList<String> cmdString(String command){
boolean success = false;
ArrayList<String> result = new ArrayList<String>();
String line = "";
try{
Process p = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {result.add(new String(line));}
BufferedReader input2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = input2.readLine()) != null) {result.add(new String(line));}
int exitvalue = p.waitFor();
if (exitvalue != 0){
System.out.println("error!!");
success = false;
}
else{
success = true;
}
for(String s : result){
System.out.println(s);
}
input.close();
input2.close();
return result;
} catch(Exception e) {
e.printStackTrace(System.err);
System.out.println("error!!!");
return result;
}
}
}
Basically. 基本上。 In the example above, the error isn't really visible. 在上面的示例中,该错误不是真正可见的。 But in my program, it is executing everything correctly the first time I run it . 但是在我的程序中,它在我第一次运行时可以正确执行所有操作。
Then... when I run it a second or third time, an error occurs. 然后...当我第二次或第三次运行它时,会发生错误。 Also... it seems that when I wait for a while before running it again, no error seems to occur. 另外...看来,当我等待一段时间再重新运行时,似乎没有发生错误。
How would I solve this issue? 我该如何解决这个问题?
This is what I think the answer is, and I'm sorry that it would have been impossible to answer my question based only on the code I gave. 我想这就是答案,很抱歉,仅根据我提供的代码不可能回答我的问题。
Basically what happened was this. 基本上发生了什么事。
I believe this is what happened. 我相信这就是发生的事情。
I discovered the error when I saw the exact line that triggered the error. 当我看到触发错误的确切行时,我发现了错误。 I was able to see that the normal stream did not produce the error, but the error stream did. 我能够看到普通流不会产生错误,但是错误流会产生错误。 I thought this was weird, and I instantly remembered that my C++ application did not have an error stream. 我以为这很奇怪,我立刻想起了我的C ++应用程序没有错误流。
Let me know if this answer makes sense! 让我知道这个答案是否合理!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.