0

I need to execute the "time" command in command prompt using java. The problem with it is that after displaying the time, it asks for a new time to be set. I can execute commands like "dir" or "cd" or "ver" normally. But those commands which ask for an input from the user like "date" or "time" are not able to execute completely. Here is the code:

try {
     Process p = Runtime.getRuntime().exec("cmd /c time");
     p.waitFor();
     BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
     String line = reader.readLine();
     while (line != null) {
         System.out.println(line);
         line = reader.readLine();
     }
} catch (IOException e1) {} catch (InterruptedException e2) {}

I suspect that since the cmd is asking for an input, hence it cannot be read by the InputStream because it thinks that stream has not ended yet, and thus the program never stops executing. So, what i am looking for is a way to enter the new time when it asks me to and then print the output of that if there is any.

5
  • Could you try adding the /t option to the executed batch command? Commented Oct 30, 2016 at 7:27
  • Why? Why not use the APIs that come natively in Java to get the time? Commented Oct 30, 2016 at 7:34
  • @MarioSantini that would not allow the user the enter new time. And I want to be able to enter the new time. Commented Oct 30, 2016 at 8:06
  • @JoeC because along with date and time, I also want to execute other commands which are not inluded in java API and which ask for an input duting their execution. Commented Oct 30, 2016 at 8:06
  • @RaghavSharma I think you should implement this with Java, then. Commented Oct 30, 2016 at 8:11

1 Answer 1

1

If your only concern is the first output then you should not wait for the process to exit (p.waitFor()) instead you proceed to get the inputstream and read the line e.g. code below.

try {
        String [] commands = {"cmd.exe","/C","time"};
         Process p = Runtime.getRuntime().exec(commands);
         OutputStream out = p.getOutputStream();
         BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
         String line = reader.readLine(); // read the first line
         System.out.println(line);

         // write to ouput
         out.write("sample".getBytes());
         out.flush();
         line = reader.readLine();
         System.out.println(line);
         line = reader.readLine();
         System.out.println(line);
         p.destroy();
    } catch (IOException e1) {}
Sign up to request clarification or add additional context in comments.

2 Comments

this would not allow me to enter the new time. And I want to be able to enter the new time.
you just basically need the outputstream and write your new time into it, I updated the code to show you what i mean

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.