1

I'm trying to run some command line operations through JAVA. One of my command requires enter to be pressed to complete. I don't know how to pass enter through java in middle of a command execution.

    import java.io.BufferedReader;
import java.io.InputStreamReader;

public class CommandLineMethods {
public static String executeCommand(String []command)
{
    StringBuffer output = new StringBuffer();
    Process p;
    try{

                 p=Runtime.getRuntime().exec(command);

        p.waitFor();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";
        while((line=br.readLine())!=null)
        {
            output.append(line + "\n");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return output.toString();
}
public static void main(String...args)
{   
    String scriptsPath ="C:\\bip_autochain\\win64_x64\\scripts";
    String scriptName="lcm_cli.bat";
    String scriptArguments="lcmproperty C:\\TestNG_Auto\\resources\\LCMBiar_Import.property";

    String []command = {"cmd.exe", "/c"," cd "+scriptsPath+" && "+ scriptName +" -"+scriptArguments};
    String res = executeCommand(command);
    System.out.println(res);

}

}

Last command which is running a script with some argument requires an enter to pressed to complete. How to achieve so?

shubham gupta
  • 321
  • 3
  • 9
  • 23
  • try to give a `\n` when it prompt for enter. – bhansa Jun 10 '16 at 06:27
  • no it's not printing anything! It's waiting for the command to execute completely before printing. but that requires an enter in between – shubham gupta Jun 10 '16 at 06:29
  • Possible duplicate of [Press a key with Java](http://stackoverflow.com/questions/11442471/press-a-key-with-java) – bhansa Jun 10 '16 at 06:31
  • it's not a duplicate of this as it requires key to be pressed within previous command, any new command won't be executed because program is waiting for the earlier command to complete – shubham gupta Jun 10 '16 at 06:35

2 Answers2

0

It looks like you'll need to create a Thread and send the key inside of it. For a simple coordination just send the child to sleep.

malarres
  • 2,941
  • 1
  • 21
  • 35
0

You can get a handle on the OutputStream for the Process and send your command that way. It might take some refactoring of your code so you can send the command at the right time (maybe when you see something in particular on the InputStream, but something like this:

Process process = Runtime.getRuntime().exec(command)
// ... stuff happens, reading the input maybe
OutputStream out = process.getOutputStream();
out.write("\n");
out.close();

There's a few caveats when you interact with a Process, particular with regards to promptly reading the output and error streams, and using threads so you can read them both at the same time. Have a look at this article for some tips:

http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html

Matt
  • 3,677
  • 1
  • 14
  • 24
  • I gave thread.sleep(10000) so that the command can reach at the right time but the out.write method doesn't take string as input it's either int or byte array. so i tried converting "\n" to byte array and passed it to the out.write method. This didn't work, my program is still stuck. – shubham gupta Jun 10 '16 at 08:29