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):
.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.
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.
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 …]
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With