How to run DOS command in JAVA with Administrator privilege?

4

Hello, I have a program where I need every run of it, go to the server, pick date and time, and change it on the local PC.

I am running the following command line:

Runtime.getRuntime().exec('DATE '+dataServidor.trim());

and JAVA returns me the following exception:

  

java.io.IOException: Can not run program "date":

I ran the command line by CMD , and it returned the following result:

  

The client does not have the necessary privilege

But when I run the command line in CMD as ADMIN , it executes normally and changes the date ...

So I think I need to run the Runtime.getRuntime () .exe () as an administrator too, but I do not know how!

    
asked by anonymous 24.11.2017 / 14:47

1 answer

-1

You can create a .bat file that receives the value to be used for the DATE command and then create a shortcut to the .bat.

Then you must configure the shortcut to be run with administrator privilege (under Properties / Advanced / Run as Administrator).

Then you use this shortcut (nickname.lnk):

Runtime.getRuntime().exec("cmd /c start \"\" \"atalho.lnk\"" + dataServidor.trim());

So only the .bat execution will have the privileges and you narrow the risk a little.

    
05.01.2018 / 21:30