Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get 'Run as administrator' in jar applications

Tags:

java

I have a desktop java application that uses Swing as a GUI library. There is an installer that I have to install inside this application, but it must have administrative privileges.

I am using

Process p = Runtime.getRuntime().exec(pathToTheExeInstaller);

to install the program. But it has this error when running it without administrator privilege:

error

Is there a way to get Run as administrator in the context menu (the right click menu) in the generated jar file when clean and build is used in NetBeans:

enter image description here

like image 575
Tarek Avatar asked Oct 17 '22 14:10

Tarek


1 Answers

This is what you need,

Runtime.getRuntime().exec("powershell.exe Start-Process 'pathToTheExeInstaller' -verb RunAs");

This will give a prompt to the user asking permissions to get elevated access for your installer.

Extracted from this answer


Example

Runtime.getRuntime().exec("powershell.exe Start-Process 'notepad.exe' -verb RunAs");

enter image description here

like image 117
Roshana Pitigala Avatar answered Oct 21 '22 01:10

Roshana Pitigala