0

I want to execute a command in the cmd from java. I want to execute link41b.exe in cmd from java. Normally it work like that :

  1. I write the path of the link41b.exe
  2. I execute link41b.exe
  3. I wait until she excute this application and it give me a result .
  4. I write the sentence that I want to link it

For example: linkparser>""the sentence""

So to execute this command I have written this code.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;

public class tistlink {

public static void main(String[] args) {

        Process child;
        String line;
        try {

        String command="cmd /c link41b.exe";
            child = Runtime.getRuntime().exec(command);
            child.waitFor();
             OutputStream out = child.getOutputStream();
             PrintStream printStream = new PrintStream(out);
             printStream.println(" the girl is beautifull");

             System.out.println(child.exitValue());

         BufferedReader input =new BufferedReader(new     
                     InputStreamReader(child.getInputStream()));          
          while((line = input.readLine()) != null)
         {System.out.println(line); }
        } catch (IOException e) {   
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }}

}

The method exitvalue() returns 1. But it didn't return the result.

What it is the problem of this code because it gave me any error?

demongolem
  • 9,474
  • 36
  • 90
  • 105
  • There is a race condition, I beleive. `link41b.exe` is done before you get a chance to read the output stream. – ixe013 May 01 '14 at 12:38
  • but I have written child.waitFor()!!! – user3490182 May 01 '14 at 13:05
  • Instead of writing and reading serially. initiate a new thread for reading and then write to the printstream. – Sanjeev May 01 '14 at 13:11
  • exitValue() of 0 is normal termination. 1 is error. So there is an error going on with the executed process. Check this link41b to see what exit value of 1 means since it is application specific. – demongolem May 01 '14 at 14:10
  • Also, check http://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki/14165567#14165567 for I/O with Process. Near duplicate, but not necessarily., – demongolem May 01 '14 at 14:13
  • You called `waitFor` [to soon](http://stackoverflow.com/q/5483830/591064). You are trying to read a stream that does not exist anymore. – ixe013 May 01 '14 at 14:39
  • @demongolem I know 1 mean the dictionary file doesn't exit but the problem that it work with the cmd because i have translate the link41b.exe to the folder of dictionnaryfile . – user3490182 May 01 '14 at 15:14

0 Answers0