Java exec使用输入重定向 [英] Java exec use input redirect

查看:130
本文介绍了Java exec使用输入重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  String cmd =D:/ James / 1 ASU / REU / senna-v3.0 / senna / senna-win32.exe -posvbs< \D:/ James / 1 ASU / REU / senna-v3.0 / senna / tmp.tmp \; 

进程p2 = Runtime.getRuntime()。exec(cmd);

我想运行一个应用程序,并从一个文本文件中输入。我试过上面的应用程序运行,但应用程序抱怨说,<不是有效的命令行参数。


无效的参数: D:/ James / 1 ASU / REU / senna-v3.0 / senna / tmp.tmp



SENNA Tagger(POS - CHK - NER - Ronan Collobert 2009


我如何重定向文件的输入?我需要从应用程序读取输出流,我已经做了:

  p2.waitFor(); 
char [] cbuf = new char [1024];
BufferedReader processOutput = new BufferedReader(new InputStreamReader(p2.getInputStream()));
processOutput.read(cbuf);
processOutput.read(cbuf);
System.out.println(new String(cbuf));

我不想运行程序并从stdin发送文本输入。我只想运行程序一次,等待它完成,然后读取所有的输出。主要原因是因为应用程序可能需要不确定的时间来完成,我不想处理如果没有输出等读取将阻塞的问题等。

解决方案

为什么Java不提供这样做是超越我的库。感谢吉姆识别我需要线程阅读为java喜欢让一切阻塞lol ...

  / * 
*要更改此模板,请选择工具|模板
*并在编辑器中打开模板。
* /
package sennaparser;

import java.io. *;
import java.util.logging.Level;
import java.util.logging.Logger;

/ **
*
* @author james
* /
public class SennaReader {
public String pathToSenna =;


public String [] getReadOut(String input)throws IOException,InterruptedException
{
Runtime rt = Runtime.getRuntime();
String s =cmd / C senna-win32.exe< tmp.tmp;
// s =cmd / C dir;
FileWriter writer = new FileWriter(pathToSenna +/tmp.tmp);
writer.write(input);
writer.close();
Process p;
String [] params = new String [1];
params [0] =;
File f = new File(pathToSenna);
p = rt.exec(s,params,f);

BufferedReader processOutput = new BufferedReader(new InputStreamReader(p.getInputStream()),500000);
BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
ReadThread r = new ReadThread(processOutput);
Thread th = new Thread(r);
th.start();
p.waitFor();
r.stop();
s = r.res;

p.destroy();
th.join();
return s.split(\\\
);
}


public class ReadThread implements Runnable {

BufferedReader reader;
char [] buf = new char [100000];
String res =;
boolean stop;
public ReadThread(BufferedReader reader)
{
this.reader = reader;
stop = false;
}

@Override
public void run(){
res =;

while(!stop){
try {
reader.read(buf);
res + = new String(buf);

} catch(IOException ex){
Logger.getLogger(SennaReader.class.getName())。log(Level.SEVERE,null,ex);
}
}
}

public void stop()
{
stop = true;
}
}

}


String cmd=" D:/James/1 ASU/REU/senna-v3.0/senna/senna-win32.exe -posvbs < \"D:/James/1 ASU/REU/senna-v3.0/senna/tmp.tmp\"";

Process p2 = Runtime.getRuntime().exec(cmd);

I want to run an application and push input into it from a text file. I tried the above and the application ran, but the application complained and says "<" is not a valid command line argument.

invalid argument: < D:/James/1 ASU/REU/senna-v3.0/senna/tmp.tmp

SENNA Tagger (POS - CHK - NER - SRL) (c) Ronan Collobert 2009

How the heck do I redirect input from a file? I need to read the output stream from the application as well, which I have done with:

p2.waitFor();
char[] cbuf = new char[1024];
BufferedReader processOutput = new BufferedReader(new InputStreamReader(p2.getInputStream()));
        processOutput.read(cbuf);
processOutput.read(cbuf);
System.out.println(new String(cbuf));

I do not want to run the program and send text input from stdin. I just want to run the program once, wait for it to finish and then read all of the output. The main reason for this is because the application may take an indeterminate amount of time to finish and I don't want to deal with the issue that reading will block if there is no output, etc..

解决方案

Why Java does not provide a library that does this is beyond me. Thanks to Jim for identifying that I need threads to read as java likes to make everything blocking lol...

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sennaparser;

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author james
 */
public class SennaReader {
    public String pathToSenna = "";


public String[] getReadOut(String input) throws IOException, InterruptedException
{
    Runtime rt = Runtime.getRuntime();
    String s = "cmd /C senna-win32.exe < tmp.tmp";
    //s = "cmd /C dir";
    FileWriter writer = new FileWriter( pathToSenna + "/tmp.tmp");
    writer.write(input);
    writer.close();
    Process p;
    String[] params = new String[1];
    params[0] = "";
    File f = new File(pathToSenna);
    p = rt.exec(s, params, f);

    BufferedReader processOutput = new BufferedReader(new InputStreamReader(p.getInputStream()), 500000);
    BufferedWriter processInput = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    ReadThread r = new ReadThread(processOutput);
    Thread th = new Thread(r);
    th.start();
    p.waitFor();
    r.stop();
    s = r.res;

    p.destroy();
    th.join();
    return s.split("\n");
}


public class ReadThread implements Runnable{

    BufferedReader reader;
    char[] buf = new char[100000];
    String res = "";
    boolean stop;
    public ReadThread(BufferedReader reader)
    {
        this.reader = reader;
        stop = false;
    }

    @Override
    public void run() {
    res = "";

        while (!stop) {
            try {
                reader.read(buf);
                res += new String(buf);

            } catch (IOException ex) {
                Logger.getLogger(SennaReader.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void stop()
    {
        stop = true;
    }
}

}

这篇关于Java exec使用输入重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆