以管理员身份从java启动命令提示符(CMD) [英] Start Command Prompt (CMD) from java as Administrator
问题描述
我正在开发一个java项目。此项目需要管理员权限才能在系统中执行一些进程。这是有什么办法在启动项目时给予管理员权限?有什么方法可以用java的管理员权限启动命令提示符?
I am working on a java project. This project need admin privileges to execute some process in the system. For this is there any way to give admin privileges while starting the project? Is there any way to start command prompt with admin privileges from java?
推荐答案
我有两种推荐方式:
使用 runas 计划
Use the runas program
这是最简单的方法,由于你真的想要一个控制台窗口的密码提示不显得尴尬。另一方面,用户可能会发现在控制台窗口中输入密码可疑。
This is the easiest way and since you actually want a console window the password prompt doesn't look awkward. On the other hand a user might find it suspicious to enter his password into a console window.
使用 CreateProcessWithLogonW API函数
Use the CreateProcessWithLogonW API function
这需要使用Win32 Java库。 (我建议 JNA )。这是一个更多的工作,但它可能会导致更好的用户体验,因为标准的Windows登录对话框。
This requires you to use a Win32 Java library. (I'd recommend JNA). It is a little more work but it might lead to a better user experience because of the standard Windows login dialog.
这篇关于以管理员身份从java启动命令提示符(CMD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!