passing input to a process
I'm working on a biology project that requires me to run little apps from java. The code below was borrowed and works 99% of the time. I send it a command which it executes like typing it into command line. The problem here is that this program requires text input. In linux, if i type in "wine protdist.exe < protdist" where protdist is a text file with the input (in this case, only "y" then carriage returns), the program runs and I get output. If I use the code below, it will execute wine protdist.exe, but I cant for the life of me pass the program the few characters that I need for it to move on. Any suggestions on how I can execute this program. send it a character and a carriage return and more on? TIA
Code :
File dirFile = new File(tempDir);
String[] cmd = new String[1];
cmd = "wine protdist.exe < protdist";
deleteFile(tempDir + "outfile");
Process coffee = null;
Runtime runtime = null;
try {
coffee = runtime.getRuntime().exec(cmd, null, dirFile);
}
catch (IOException e) {
e.printStackTrace();
System.out.println("OMG FAIL!");
}
InputStream is = coffee.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
deleteFile(tempDir + "testOuput.txt");
TextIO.writeFile(tempDir + "testOutput.txt");
try{
while ((line = br.readLine()) != null) {
TextIO.putln(line);
}
}
catch (IOException e) {System.out.println("fail");}
//System.out.println("Program terminated!");
Re: passing input to a process
Re: passing input to a process
If the file that is to capture the output from the program already exists, it just deletes it
Re: passing input to a process
Have you printed out the error Stream to see if the called program is complaining about anything?
Re: passing input to a process
Could someone explain to me why this is working:
Code :
Runtime runtime = null;
try {
coffee = runtime.getRuntime().exec(cmd, null, dirFile);
}
catch (IOException e) {
e.printStackTrace();
System.out.println("OMG FAIL!");
}
Because what I see is that a var called "runtime" is initialized and we know its of type Runtime, but its null, so no object. How can he execute the command runtime.getRuntime().exec(cmd, null, dirFile) if the runtime is null?
Re: passing input to a process
Quote:
public static Runtime getRuntime()
When in doubt read the API doc. It says that getRuntime() is static. That means you don't need an object to call it.
Re: passing input to a process
After trying a thousand variations on buffered writers, this seems to work. Don't ask me why, but I thought write should output arguments to the program, but it never did until I added the flush. So, for today, I give up figuring this thing out b/c I have something that works.
Code :
try{
String arguments = "y" + "\n" + "\n";
coffee.getOutputStream().write(arguments.getBytes());
coffee.getOutputStream().flush();
}
catch (IOException e){System.out.println("fail");}
Re: passing input to a process
Quote:
Originally Posted by
Norm
Have you printed out the error Stream to see if the called program is complaining about anything?
Btw, I hadn't tried this, but it was clear from the program output that it was just hanging b/c it wasnt getting the input.
Re: passing input to a process
Quote:
Originally Posted by
Norm
When in doubt read the API doc. It says that getRuntime() is static. That means you don't need an object to call it.
I know about static, that was also my first though(but forgot to check the doc:P) but then again, why initialize the var? if its static it doesn't need that right?
Something along the next code would also work right?
Code :
coffee = Runtime.getRuntime().exec(cmd, null, dirFile);