How to run DOS command in JAVA with Administrator privilege?

Asked

Viewed 861 times

5

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

I am running the following command line:

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

and JAVA returns me to the following Exception:

java.io.Ioexception: Cannot run program "date":

I circled the command line by own CMD, and returned me the following result:

The client does not have the necessary privilege

But when I run the command line on CMD as an administrator, it performs normally and changes the date...

So I believe I need to execute the Runtime.getRuntime(). exec() as an administrator too, but I don’t know how to do!

  • 3

    It would be wrong to be able to execute things as an administrator... it would be flawed, already noticed that when it goes to install a program that needs privileges it asks permission? then... only with permission at all

  • Just run the parent process as administrator, all the privileges will be passed automatically.Run the JAR as administrator, the command will work.

1 answer

-1

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

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

Then you use this shortcut (nameDoAtalho.lnk):

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

So only running the . bat will have the privileges and you restrict the risk a little.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.