1

I managed to change system time and date using runtime in java. However I have to run this two commands one at a time opening two command prompts instead of one because if run both commands at the same time the command prompt gets them as one invalid command

//this is the working code that opens two cmd`s:

Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /c time 11:30");
rt.exec("elevate.cmd cmd.exe /c date 02-04-2012");

//this is the code that I think it should open one cmd and execute both of the commands

Runtime rt = Runtime.getRuntime();
rt.exec("elevate.cmd cmd.exe /C time 11:25 /C date 02-05-2012");

But the cmd is returning "The system cannot accept the time entered". Note: the elevate.cmd is a batch file I use it to run the cmd as administrator(win7) and you can download it from here.

How can I make the system change both time and date by opening cmd once? Or what other choices do I have?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Erol E.
  • 41
  • 2
  • 5
  • 2
    Did you try to run both commands in the same line with '&' or '&&' characters? – acanimal Feb 05 '12 at 12:25
  • It didn't work...the parameters of exec should be String or String[] and the operator '&&' or '&' can't be used for string variables – Erol E. Feb 05 '12 at 14:53

1 Answers1

0

Try solutions from this SOq:

Basically, create a process and then "write" commands to it as if you were an user typing them. I don't have Win7 to test (don't know how it will behave in combination with elevated privilege prompts), but it works for me on Ubuntu 11.10 - hope it works for your case, too.

Alternatively, you can make another .cmd file (called e.g. changedt.cmd) that will contain the two commands:

elevate.cmd cmd.exe /c time 11:30
elevate.cmd cmd.exe /c date 02-04-2012

and then run it instead:

Runtime rt = Runtime.getRuntime();
rt.exec("changedt.cmd");

This should work in any case, as you seem to be successfully running pretty much the same thing, but adds another .cmd file.

Hope this helps.

Community
  • 1
  • 1
icyrock.com
  • 27,952
  • 4
  • 66
  • 85
  • Ok,thank you for your answer.I did as you said,using another .cmd file with the full command that neil recommended to me in his comment.I was trying to do several things at once,but since its working,its good for me.Thanks again to you all! – Erol E. Feb 06 '12 at 10:55