How do I easily elevate when running a .jar file?

16,450

Solution 1

To make it easy for others to run the JAR installer without having to make changes to all their computers, you'll probably want to wrap it with a batch script that elevates and runs the JAR file. You can do this easily with the Elevation PowerToys from Microsoft. They include a useful utility that lets you launch anything as administrator by simply prefixing it with the elevate command.

Once you've downloaded them, extract them to a folder and copy the elevate.cmd and elevate.vbs to the folder with the JAR file. Then, write a new batch script with a .cmd extension with the following contents:

elevate cmd /c start "%CD%\installer.jar"

That will launch the JAR file with the default handler for such files on their system (using the start command of cmd). %CD% points to the current working directory of the script, and is necessary because the elevated command line will start in c:\windows\system32.

If, for some reason Java is not properly configured as the default handler for JAR files, that would fail though. If you can count on Java being in the same place on all systems, you could instead do it this way:

elevate "c:\Program Files\Java\jre\bin\java.exe" -jar "%CD%\installer.jar"

Once you've done either of those, just run the batch file and Windows will prompt for administrator elevation and launch the installer.

Include the elevation scripts, your script, and the installer files in a self-extracting archive that launches the script and you can wrap it up in one nice little .exe file. (7-Zip is a great open-source tool for creating such archives.)

For more information on the Elevation PowerToys, consult the linked article or the README file included with the download.

Solution 2

I've just had occasion to do this, and found that the easiest way, without having to download and install Elevation PowerToys, was to run cmd.exe as an administrator and then call java.exe -jar 'installer.jar' from the command line. Of course, I had to find java.exe first, but that didn't take long...

Share:
16,450

Related videos on Youtube

Merlyn Morgan-Graham
Author by

Merlyn Morgan-Graham

Game Development / Game Engines • C • GLSL • C++ • C# Working at Shiny Shoe games as a developer (console ports of Monster Train. Xbox One/Win Store ports of Grim Fandango, Full Throttle, Day of the Tentacle) Past: Producer/dev manager/dev on Receiver 2 and Overgrowth at Wolfire Games Product manager and technical lead at a 120-headcount web startup Dev manager, web dev, test automator (at Launch Consulting and Microsoft)

Updated on September 18, 2022

Comments

  • Merlyn Morgan-Graham
    Merlyn Morgan-Graham over 1 year

    When trying to run an installer Jar file, I am getting an error saying that write access is denied to create a directory under the Program Files folder.

    Right click -> Run as Administrator is not available on Jar files (I assume because it is Java.exe that consumes them - they are not themselves treated as directly executable by the shell).

    What is the quickest and simplest way to run a .Jar file with elevation?

    I am evaluating this tool to recommend for our dev team, and they will manually install it on their boxes. I'd prefer an option that doesn't require them to type anything.

  • Merlyn Morgan-Graham
    Merlyn Morgan-Graham about 13 years
    Awesome answer :) Thanks! I'm going to wait to accept it so maybe the question/your answer gets some more traffic (I think it works that way?)
  • Pacerier
    Pacerier over 8 years
    Actually you only need to call installer.jar. It'll automatically locate the default "open with" program which has already been set to java.exe. (If it hadn't, double-clicking the .jar wouldn't run it in the first place.)
  • Pacerier
    Pacerier over 8 years
    @Patches, What's the non 3rd-party way to do this?
  • Patches
    Patches over 8 years
    @Pacerier Even Windows 10 still has no built-in method of elevating from the command line.