0

I want to run a shell script from a java program. This shell script invokes a system library which needs a big file as resource.

My java program calls this script for every word in a document. If I call this script again and again using Runtime.exec() the time taken is very high since the resource loading takes lot of time.

To overcome this I thought of writing the shell script as follows (to make it run continuously in background ):


count=0

while count -lt 10 ; do

  read WORD

  //execute command on this line

done

I need retrieve the output of the command in my java program and process it further. How should I code the I/O operations for achieving this task?

I have tried writing words in to the process's output stream and reading back output from process's input stream. But this does not work and throws a broken pipe exception.


try {

    parseResult = Runtime.getRuntime().exec(parseCommand);

    parsingResultsReader = new BufferedReader(new InputStreamReader (parseResult.getInputStream()));

    errorReader = new BufferedReader(new InputStreamReader (parseResult.getErrorStream()));

    parseResultsWriter = new BufferedWriter(new OutputStreamWriter((parseResult.getOutputStream())));

} catch (IOException e) {

            e.printStackTrace();

}

parseResultsWriter.write(word);

parseResultsWriter.flush();

while ((line = parsingResultsReader.readLine()) != null) {

     // capture output in list here

}

Kindly help with this issue

Romain
  • 12,679
  • 3
  • 41
  • 54
  • Does the "shell" process need to be independent from the VM, or could the VM start it by itself? – Romain Jun 28 '12 at 11:24

1 Answers1

0
//execute command on this line

Is this command a separate program? Then it will be launched for every word, so you'll get rid of only shell process which is lightweight anyway. You have to learn how to run the heavyweight command for many words at once.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38