简体   繁体   English

使用Runtime.exec时读取错误

[英]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. 基本上发生了什么事。

  1. My cmdString() method in my program was initiating a process and trying to read from its output stream and its error stream. 我程序中的cmdString()方法正在启动一个进程,并尝试从其输出流和错误流中读取。
  2. It so happens that the Process that my program was initiated, was a C++ application designed by me which used custom pipes to redirect input and output. 碰巧我的程序启动的过程是我设计的C ++应用程序,它使用自定义管道重定向输入和输出。
  3. This C++ application had a pipe for standard output, but I happened to not make one for standard error. 这个C ++应用程序有一个用于标准输出的管道,但是我碰巧没有为标准错误创建一个管道。
  4. Because I did not create a standard error pipe, my cmdString() method was trying to read from a pipe that I didn't create. 因为我没有创建标准错误管道,所以我的cmdString()方法试图从我没有创建的管道中读取数据。 So therefore I got the error on the line "line = input2.readLine())" which corresponded to the error stream. 因此,我在与错误流相对应的“ line = input2.readLine())”行上得到了错误。
  5. So in summary, the error was due to trying to read an Error Stream from a process that did not produce an error stream. 因此,总而言之,该错误是由于尝试从未产生错误流的进程中读取错误流所致。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM