Can Java launch the Windows UAC?

前端 未结 4 1021
眼角桃花
眼角桃花 2021-02-10 07:40

As the title says, I\'m wondering if it is possible for a program written in Java (and only java) to relaunch himself (preferably a .jar) with administrator privileges, s

4条回答
  •  借酒劲吻你
    2021-02-10 08:07

    To relaunch your application elevated, you have to call ShellExecute or ShellExecuteEx function from Windows API and use runas verb.

    You can use these API in pure Java with JNA library.

    To relaunch yourself, you would have to know the full path to java.exe or javaw.exe, the command-line parameters (class path, if any, and the path to your jar). Obviously you can get this information by using Windows API.


    What do you mean by remote case?
    You cannot start remote elevated process this way.

    You can re-launch your application elevated from a network share. Yet it won't work with mapped drives: after elevation there's no access to user's mapped drives.


    No, this can't work on other platforms. UAC is a Windows feature. It's similar to sudo in Linux in some ways, so for Linux you can use sudo $pathtojava/java.exe . However this won't work nicely if your application is not started from a console. Window Managers usually have wrappers which prompt for password in a GUI dialog.

提交回复
热议问题