8

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.

2 Answers 2

6

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.

3
  • 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?) Mar 26, 2011 at 7:39
  • @Patches, What's the non 3rd-party way to do this?
    – Pacerier
    Sep 8, 2015 at 10:32
  • @Pacerier Even Windows 10 still has no built-in method of elevating from the command line.
    – Patches
    Oct 1, 2015 at 0:03
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...

1
  • 2
    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
    Sep 8, 2015 at 10:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .