Execute jar file as Administrator in Windows

30,056

Solution 1

Well, you have two options.

First: open CMD as administrator and open jar:

Run command prompt as administrator first.

Start > cmd > right click > run as administrator.

Run the jar file using

java -jar c:\path\to\jar\file.jar

Second: create a .bat file which asks for admin permissions

@echo off
:: BatchGotAdmin (Run as Admin code starts)
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
exit /B
:gotAdmin
if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
pushd "%CD%"
CD /D "%~dp0"
:: BatchGotAdmin (Run as Admin code ends)
:: Your codes should start from the following line
java -jar c:\path\to\jar\file.jar

Note that you should only change the last line.

I would place both the .jar and .bat files in the same folder, and only change the last line to:

java -jar ./file.jar

Answer based on this page.

Hope this helps!

Solution 2

Maybe everyone knows this already, but . . .

  1. Create a batch file that just runs the JAR file
  2. Save the batch to the user's document folder, right-click and use "Send To > Desktop (Create Shortcut)"
  3. Right click the shortcut go to properties and click the "Advanced" button.
  4. Choose run as admin. Lots of things do not allow run as admin, but batches always do, and when I say batches, I mean shortcuts to batches.
Share:
30,056
FiroKun
Author by

FiroKun

I´m a student in the first year of Engineering in Computer Science in UCI, Cuba. I like programing a lot! And program anything from desktop application to websites... Is like a hobby form me rather than a subject on the university or a work. Also my other passion is graphic design which is how I make my living, specially UI/UX. Love those!

Updated on May 14, 2021

Comments

  • FiroKun
    FiroKun almost 3 years

    I know there are tons of questions here asking the same, but in any of those ain't found the answer that I want, that is a specific way for doing this.

    I know that (correct me if I'm wrong):

    1. For doing this, i'll need to interact with Windows UAC mechanism.
    2. This can't be done with pure Java code, it needs the help of a Batch file, or VBS script.
    3. A Java running process can't be elevated to get admin privilegs without needed restart the application.
    4. Elevate a .jar file will elevate also the JVM and all other process that depends of JVM, with the security concern this imply.

    I don't want to restart any application, my goal is that the Java application (.jar file) starts from the begining with admin privilegs. If for get that, the users will have to click in some UAC windows, ok, don't care.

    So, my question (that I can't get and specific answer reading other post asking almost the same thing). How can I do that?, What files I need to create, and with what content? (.bat or .vbs), Can I put those files inside my .jar file?, What Java code I'll need to implement in my app?

    Please, be the most specific as possible with the solution. Answers in other post are, or too vague (talk about "possible" solutions, but don't mention a specific and complete one), or explain too much and don't give a specific way or code.