Click here to Skip to main content
15,997,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
// this the c++  code for the hello.exe
#include<iostream.h>
#include<conio.h>
int  main()

{
 cout<<"Hello world\n";
 getch();
 cout<<"i bypass eroor\n";
  return 0;
 }
 // i need to run the hello.exe from the java program


i want to learn java in order to call the sub process bin file and send control execution by the io to those bin files
like in this hello.exe it will print the hello world and if i and able to enter the value then only i am able to see the i bypass eroor.

now this is my java program

Java
package procs;
    import java.io.*;
    import java.lang.*;
    import java.util.*;
    /**
    *
    * @author prodesign
    */
    public class Procs {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
                      
    Scanner scan = new Scanner(System.in);     
              
        try
    {
      int exitVal;
      char c='a';
     Process process = Runtime.getRuntime().exec(new String[]{"C:/hello.exe"});
     OutputStream stdin = process.getOutputStream ();
     InputStream stderr = process.getErrorStream ();
     InputStream stdout = process.getInputStream ();

      BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
      //PrintStream writer = new PrintStream(new BufferedOutputStream(stdin));
      //BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
      // i uses both printstream and Buffered writer
 
    System.out.println("lets start<<<<<<");
    do
    { c=(char)reader.read();
    System.out.print(c);
 
    }while(c!='\n');
    //writer.Print(c);  i uses  both print
    //writer.write(c);  and the writer
    writer.flush();
    do
    { c=(char)reader.read();
    System.out.print(c);
 
    }while(c!='\n');
    exitVal=process.waitFor();
    System.out.println("Exited with error code "+exitVal);
  
    }catch (Exception e)
    {
     e.printStackTrace();
      }
    }
    }


please help me
it hangs after the lets start<<<<<
it does not even show hello world
also help me with this part that

Java
exitVal=process.waitFor();


like this does we have some pause command to pause the execution of the process

need Rx !!!!
Posted
Comments
Sergey Alexandrovich Kryukov 24-May-13 16:17pm    
What do you call a "bin" file, exactly. All files are "binary", did you know that? :-)
...OK, I can see: "C:/hello.exe"; this must be PE executable file...
—SA
Prasad Khandekar 24-May-13 23:44pm    
Hllo Shubham,

PLease see this thread on Stackoverflow (http://stackoverflow.com/questions/9305413/java-exec-use-input-redirect) Ot this (http://stackoverflow.com/questions/3643939/java-process-with-input-output-stream)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900