Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute jar file as Administrator in Windows

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.

like image 283
FiroKun Avatar asked May 08 '16 21:05

FiroKun


People also ask

How do I run a .JAR file in Windows 10?

Right-click your JAR file and choose Open With > Java(TM) Platform SE Binary. Tip: If you don't see that option in the “Open With” menu, then click “Choose Another App” and you'll see the option. Your JAR file will launch and you'll be able to interact with it. And that's all there is to it.

How do I run a JAR file in Windows with arguments?

Run a Nonexecutable JAR with Arguments To run an application in a nonexecutable JAR file, we have to use -cp option instead of -jar. We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …]

Can you execute a JAR file?

Most JAR files are simply containers for data that another program needs to run with Java; therefore you cannot run these files and nothing will happen when you double-click them. Similarly, most executable JAR files are downloaded as installation files to install applications or programs.


1 Answers

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!

like image 179
Edudjr Avatar answered Oct 30 '22 09:10

Edudjr