Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java exec

  • 12-05-2010 11:12AM
    #1
    Registered Users, Registered Users 2 Posts: 427 ✭✭


    Hi,
    I have a program which cannot run on windows. I have managed to get it to run in Cygwin and now I am hoping to call the program from a windows Java application using Runtime.exec. I know this is messy but I don't have the option of switching OS atm.


    So what I need is to launch the cygwin terminal and pass new commands to that terminal in order to navigate to my program and execute it. Does anyone have examples of this?

    My problem is that I can execute a command but I can't get any new output when inputting new commands.

    I'll give an example using cmd instead of cygwin:
    String line;
        OutputStream stdin = null;
        InputStream stderr = null;
        InputStream stdout = null;
    
          // launch EXE and grab stdin/stdout and stderr
          Process process = Runtime.getRuntime ().exec ("cmd.exe", new String [] {"/K"});
          stdin = process.getOutputStream ();
          stderr = process.getErrorStream ();
          stdout = process.getInputStream ();
      
          // a new command into cmd
          line = "dir" + "\n";   
          stdin.write(line.getBytes() );
          stdin.flush();
    
          stdin.close();
          
          // clean up if any output in stdout
          BufferedReader brCleanUp = 
            new BufferedReader (new InputStreamReader (stdout));
          while ((line = brCleanUp.readLine ()) != null) {
            //System.out.println ("[Stdout] " + line);
          }
          brCleanUp.close();
          
          // clean up if any output in stderr
          brCleanUp = 
            new BufferedReader (new InputStreamReader (stderr));
          while ((line = brCleanUp.readLine ()) != null) {
            //System.out.println ("[Stderr] " + line);
          }
          brCleanUp.close();
    

    I don't get any errors. It just executes with no output.
    Thanks

    Edit: I have tried using both \n and \r and both in order to indicated carriage return when writing to the cmd process.


Comments

Advertisement