从java运行交互式Dos程序

时间:2012-01-09 01:13:04

标签: java multithreading dos

我想从Web服务器运行Dos程序。 Dos程序必须以交互方式运行,因为用户界面是通过一系列问题和答案。一个问题的答案将决定下一个问题。我将不得不在Web服务器上使用ajax,但我想我可以做到这一点。

我在Stackoverflow上发现了一个java程序,它似乎做了类似于我想要的东西。但是当我编译程序时,我得到一个错误,即

javac PipeRedirection.java 
PipeRedirection.java:43: package InputProcess does not exist
                    InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream());

堆栈溢出问题网址

How can I write large output to Process getOutputStream?

Java文件是

/* 
 ####### PipeRedirection.java 
*/

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class PipeRedirection {

public static void main(String[] args) throws FileNotFoundException {

    if(args.length < 2) {
            System.err.println("Need at least two arguments");
            System.exit(1);
    }

    try {
            String input = null;
            for(int i = 0; i < args.length; i++) {

                    String[] commandList = args[i].split(" ");

                    ProcessBuilder pb = new ProcessBuilder(commandList);
                    //pb.redirectErrorStream(true);
                    Process p = pb.start();

                    if(input != null) {
                            PrintWriter writer = new PrintWriter(new OutputStreamWriter(new BufferedOutputStream(p.getOutputStream())), true);
                            writer.println(input);
                            writer.flush();
                            writer.close();
                    }

                    InputProcess.Gobbler outGobbler = new InputProcess.Gobbler(p.getInputStream());
                    InputProcess.Gobbler errGobbler = new InputProcess.Gobbler(p.getErrorStream());
                    Thread outThread = new Thread(outGobbler);
                    Thread errThread = new Thread(errGobbler);
                    outThread.start();
                    errThread.start();

                    outThread.join();
                    errThread.join();

                    int exitVal = p.waitFor();
                    System.out.println("\n****************************");
                    System.out.println("Command: " + args[i]);
                    System.out.println("Exit Value = " + exitVal);
                    List<String> output = outGobbler.getOuput();
                    input = "";
                    for(String o: output) {
                            input += o;
                    }
            }
            System.out.println("Final Output:");
            System.out.println(input);

    } catch (IOException ioe) {
            // TODO Auto-generated catch block
            System.err.println(ioe.getLocalizedMessage());
            ioe.printStackTrace();
    } catch (InterruptedException ie) {
            // TODO Auto-generated catch block
            System.err.println(ie.getLocalizedMessage());
            ie.printStackTrace();
    }

}


public static class Gobbler implements Runnable {
    private BufferedReader reader;
    private List<String> output;

    public Gobbler(InputStream inputStream) {
            this.reader = new BufferedReader(new InputStreamReader(inputStream));
    }

    public void run() {
            String line;
            this.output = new ArrayList<String>();
            try {
                    while((line = this.reader.readLine()) != null) {
                            this.output.add(line + "\n");
                    }
                    this.reader.close();
            }
            catch (IOException e) {
                    // TODO
                    System.err.println("ERROR: " + e.getMessage());
            }
    }

    public List<String> getOuput() {
            return this.output;
    }
}
}

有谁知道我为什么会收到编译错误?我可以用其他代码替换InputProcess吗?

感谢您的帮助

彼得

1 个答案:

答案 0 :(得分:1)

我认为很明显你错过了这段代码的部分内容。 OP的帖子中没有包含名为InputProcess的包,其中包含一个名为Gobbler的类。可能是因为它与他们的问题无关。

错误消息实际上表示它无法找到它正在寻找的包/代码。

这门课的确切做法,只有OP可以告诉你。但最简单的是,它似乎从InputStream读取并将其转换为List<String>。我会阅读Java IO并尝试复制类似的功能。

编辑:

看起来Gobbler类确实包含在上面的示例中。从您的代码中删除InputProcess软件包名称(或将Gobbler类放在InputProcess包中),您应该很高兴。

相关问题