简体   繁体   中英

running application from cmd as administrator through java code

I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code. r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:

  1. How to run any application from CMD in windows 8 with administrator privilliges?
  2. The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
  3. I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.

Thanks

You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.

I had a similar issue to you in the past, and I solved it using PsExec , running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.

Using your example you could run:

PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe

The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.

This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM