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);
我想运行一个应用程序并从文本文件中输入输入。我尝试了上面的内容并运行了应用程序,但应用程序抱怨并说“&lt;”不是有效的命令行参数。
无效参数:&lt; D:/ James / 1 ASU / REU / senna-v3.0 / senna / tmp.tmp
SENNA Tagger(POS - CHK - NER - SRL)(c)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发送文本输入。我只想运行程序一次,等待它完成然后读取所有输出。造成这种情况的主要原因是因为应用程序可能需要不确定的时间才能完成,我不想处理如果没有输出就会阻塞读取的问题等。
答案 0 :(得分:1)
来自Can Java Runtime.exec another java program that uses stdin?和this post:
调用方法
Process.getOutputStream
并将输入提供给返回的输出流。
...所以这看起来像:
String cmd = "D:/James/1 ASU/REU/senna-v3.0/senna/senna-win32.exe -posvbs";
Process p2 = Runtime.getRuntime().exec(cmd);
String fileInput = "D:/James/1 ASU/REU/senna-v3.0/senna/tmp.tmp";
BufferedWriter bufferedwriter = new BufferedWriter(new OutputStreamWriter(p2.getOutputStream()));
BufferedReader br = new BufferedReader(new FileReader(fileInput));
String s = br.readLine();
while (s != null)
{
bufferedwriter.write(s);
s = br.readLine();
}
bufferedwriter.flush();
...并且您希望用try
/ catch
包围FileReader
,并可能close()
将变量finally
正确分配给{{1}} {{1}} 1}}阻止。
答案 1 :(得分:0)
与Java中的单独进程交互,正确执行涉及启动两个或三个线程,每个线程对应stdin,stdout和stderr(如果需要),并处理线程上的I / O 。尝试在主线程上执行所有操作可能会导致主线程或外部进程挂起。例如,如果您首先读取进程的stdout,并且需要输入,那么一切都将停止。如果您处理首先发送输入,并且输出填满内部缓冲区,则外部进程将停止。
为每个流启动一个单独的线程,并在线程上执行所有I / O.在完成所有工作后,加入主线程与I / O线程和外部进程进行清理。
答案 2 :(得分:0)
为什么Java不提供可以做到这一点的库,这超出了我的范围。感谢Jim确定我需要线程来阅读,因为java喜欢让一切都阻塞大声笑......
/*
* 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;
}
}
}