Execute jar file as Administrator in Windows
Asked Answered
W

3

13

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.

Whitneywhitson answered 8/5, 2016 at 21:28 Comment(0)
B
27

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!

Belldas answered 19/6, 2016 at 8:0 Comment(2)
There is a lot of questions similar, here in Stackoverflow. This answer is the unique acceptable for all of them! Thank you.Bewail
Let me suggest @Edudjr, you can use start 'javaw -jar -Xms1024m -Xmx1024m your.jar'. See more here #5711339Bewail
U
1

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.
Unstep answered 13/5, 2021 at 14:49 Comment(0)
R
0

Is more simple than that. Create you jar file with a default class, so that if you do java -jar yourfile.jar, the application will start. Give it execution permission. Once all this is done, just right click your jar file and select "Start as administrator" (or something similar). And that's it.

For helping the users, write a class that checks if administrator permissions are there. If not, just prompt the user and instruct them to do the afore mentioned.

Rowan answered 8/5, 2016 at 21:34 Comment(4)
This wouldn't be a problem if the option "Start as administrator" appear in the menu when the .jar is right-clicked, option that don't appear (don't know why). The .jar is a GUI app, and I want make all this process the most transparent and automatically possible for the user.Whitneywhitson
Go to compatibility (under the context menu) and check the options. There should be an option to always open your application as administrator.Rowan
Have a look to this jpassing.com/2007/12/08/…Rowan
No, the compatibility options don't apply, since is a .jar file, not an .exe file. And I prefer not to involve any related to the Windows command prompt, I need a more fast and heavy way, that don't fail.Whitneywhitson

© 2022 - 2024 — McMap. All rights reserved.