0

Although I put a correct date, if I where to put something else and it be an error. I want to know how can I write multiple inputs to the command prompt. For example, I put "05-13-2018" as the variable for writer.write(). Is there any way underneath that code to enter a value if I wanted too and then have that execute. Please message me, if you don't understand what I am trying to achieve. Thanks again for all your help

import java.io.*;
import java.util.ArrayList;


public class command{
    public static void main(String[] args) {


        String command="cmd /c date";

        try {
            Process process = Runtime.getRuntime().exec(command);
            OutputStreamWriter outputt = new OutputStreamWriter(process.getOutputStream());


            BufferedWriter writer = new BufferedWriter(outputt);
            writer.write("05-13-2018");
            writer.close();




            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            reader.close();


        } catch (IOException e) {
            e.printStackTrace();
        }



    }
}
fabian
  • 80,457
  • 12
  • 86
  • 114
Kane01
  • 61
  • 1
  • 1
  • 7
  • Do you want to enter your input manually through command line? – Yevgen Jul 03 '18 at 20:25
  • @Eugene No, but my end goal is creating a GUI interface where i pass two values from the client into command program in a exe program that takes the host name and password, and does some computation with the info given. – Kane01 Jul 03 '18 at 20:32
  • So, eventually, you want to enter several consecutive inputs, until `The system cannot accept the date entered.` message is gone, right? – Yevgen Jul 03 '18 at 20:36
  • @Eugene Yeah close, but really I will be running a command where you enter into a directory run a command and in the command prompt it will prompt you for your hostname and then password. Thats why I asked if there is a way to do two or more inputs. – Kane01 Jul 03 '18 at 22:22
  • 1
    You need to do a `newLine()` on `writer` after writing the value. Here, it still works because you close the stream after writing the value, which forces the other side to receive it. But, of course, that can be done only once. So use `writer.write(value1); writer.newLine(); writer.write(value2); writer.newLine(); …` and close only at the end. For small values, you don’t even need to wait for the program to output the “enter …” messages, as the written data will be buffered. With interactive input, it works anyway, as the user will wait before entering the value. – Holger Jul 04 '18 at 05:51
  • @Holger Hey, thanks for the help but it still is not working, if you find time trying running the code and input the new solution you gave me. It keeps giving my the same output as before. Thanks again for your help – Kane01 Jul 04 '18 at 15:13
  • It seems, Windows is a bit special. Write a value, followed by `writer.flush();`, then read some output, then write the next value, followed by `writer.flush();`. It seems, otherwise it’s dropping unexpected data that have been written before requested. – Holger Jul 04 '18 at 15:50
  • @Holger Bro, No homo but I love you so much for helping me, I have been stuck on this problem for like 3 days. Also, I hope all your dreams come true and you get a million dollars. Lol Thanks again for everything – Kane01 Jul 04 '18 at 17:13
  • @kane01 There is a comprehensive answer to a problem that looks similar to yours, you might find it helpful - https://stackoverflow.com/a/3644288/3262990 – Yevgen Jul 04 '18 at 19:39
  • @kane01 if [my answer](https://stackoverflow.com/a/51177644/2711488) works for you, you can [accept it](https://stackoverflow.com/help/someone-answers). Otherwise, please provide feedback about the remaining problems. – Holger Jul 05 '18 at 06:39
  • @Holger Sorry about that bro, yeah it works and I am grateful but when I switch the command and run it on a batch file that I will actually be using it tends to get hung in the while loop and continually just run the Thread.sleep() method. If you have any ideas on how to fix this , it would be much appreciated. P.S I used the date command as a proof of concept, if you would like for me to provide the actually bat file I will be using just let me know, Thanks again. – Kane01 Jul 05 '18 at 11:07
  • Maybe the different commands exhibit different behavior regarding the input, so knowing the actual commands may help. – Holger Jul 05 '18 at 12:15
  • @Holger Hey Holger, I made a new stack overflow question where I give the bat file as while as the appropriate code. If you could can you look over it when your free, and let me know if you can find the error I am getting with the Thread.sleep(). – Kane01 Jul 05 '18 at 12:38

2 Answers2

0

you can always do another write

writer.write("whatever");
Nikhil Jain
  • 586
  • 6
  • 21
0

It seems that when the command tries to read a value, it will read all available input and treat it as one value. You have to write and flush the values at the right time and in that regard, avoid all buffering:

String command="cmd /c date";
try {
    Process process = Runtime.getRuntime().exec(command);
    try(Writer writer = new OutputStreamWriter(process.getOutputStream());
        Reader reader = new InputStreamReader(process.getInputStream())) {

        CharBuffer buf = CharBuffer.allocate(80);
        int tries = 3;
        while(process.isAlive()) {
            while(reader.ready() && reader.read(buf)>0) {
                System.out.append(buf.flip());
                buf.clear();
            }
            if(tries-- == 0) {
                process.destroy();
                break;
            }
            writer.write("05-13-2018");
            writer.flush();
            while(!reader.ready()) Thread.sleep(200);
        }
    }
} catch(IOException|InterruptedException e) {
    e.printStackTrace();
}
Holger
  • 285,553
  • 42
  • 434
  • 765