2

I have a little .jar that executes a simple system administration task and so it needs to be run with elevated privilege. I've been researching this for hours and now know that it can be done in three ways: 1) ran from an elevated cmd prompt 2) convert the .jar to .exe and bundle it with a manifest file 3) use another .jar to launch my .jar and ask for permission.

Option 1) won't work for me because this will need to be deployed to other users that won't know how to do this. Option 2) isn't ideal because I chose to write this app in Java for its portability. This will likely be run on different systems and Java seems the most compatible. So that leaves Option 3) and is where my question comes in. I can't seem to sift through the multitude of info out there on how to accomplish creating a wrapper for my app. With my specs in mind what do you all recommend for creating a wrapper .jar that will prompt the user to allow my .jar to run? Thanks

assylias
  • 321,522
  • 82
  • 660
  • 783
shoota
  • 111
  • 11
  • 2
    How about a simple batch file? http://stackoverflow.com/questions/1894967/how-to-request-administrator-access-inside-a-batch-file – assylias Aug 20 '14 at 20:44
  • @assylias: why can we assume windows here? If it is a windows question, see http://stackoverflow.com/questions/4662574/how-do-i-elevate-my-uac-permissions-from-java – dst Aug 20 '14 at 20:44
  • @dst You can assume Windows because you're attempting UAC elevation, which is Windows-specific. – Sneftel Aug 20 '14 at 20:46
  • 1
    @Sneftel I mainly asked because the question explicitly mentioned portability, which lead me to think of a multi-platform environment, where windows is only one of multiple platforms where elevation is needed. The windows tag was added in assylias' edit. – dst Aug 20 '14 at 20:48
  • Point 2 won't effect the portability of I the application. The exe is a wrapper which launches your Java program. This means you might have an exe which is deployed on Windows, but you could set the exe wrapper to execute the jar files as external resources ( so you'd have to deploy both the jar and exe. Do this all the time. In fact, if you really want to run on the Mac OS, you should wrap the program in .app package... – MadProgrammer Aug 20 '14 at 21:01
  • @dst the question is about how to run a jar with admin rights on windows so the answer may be a windows specific method, hence the tag. And that kind of tying is going to be platform specific anyway I'm afraid. The op can remove it if he feels it is not appropriate. – assylias Aug 20 '14 at 21:01
  • I failed to mention that this will have a GUI, that will provide the user will several different options to change at will. While it initially most likely only be used on Windows, there is a chance it will be used on other systems down the road. There really needs to be one single executable that a user can double-click on, change stuff, and then close. I hope that clears it up a little more. What say you guys to that? – shoota Aug 21 '14 at 00:09
  • @assylias can you elaborate on what I need to do to get this done? Your link didn't seem to help me much. Thanks! – shoota Aug 22 '14 at 19:30
  • @MadProgrammer Can you point me in the right direction with regards to your suggestion. It might actually be something that would work for me. Thanks – shoota Aug 22 '14 at 19:38
  • For windows, I use exe4j, y have to pay for it, but I has privilege elevation built in, but there or others. Y can also do a search for mac app bundler if your interested, shouldn't be to hard to find – MadProgrammer Aug 22 '14 at 19:54
  • @MadProgrammer Thanks. I'm not worried about bundling for Mac right now. I just need something that will allow me to execute a .jar file with elevated privileges on a PC. It shouldn't be that hard but I can't find a straightforward answer. – shoota Aug 22 '14 at 20:23

3 Answers3

1

On Windows, it is possible to run a Java application either as a desktop application, or as a Windows Service in the background. In the case of a Service, the Wrapper needs to be able to be installed, removed, started, stopped, have its status queried, etc. Depending on whether the application has a GUI or is meant to be run in a command window also determines how it will be run.

On Windows systems, the most way of launching the Wrapper is to make use of dedicated batch files to perform each action of controlling the Wrapper. This makes it possible for the end user to double click on the batch file icons or set up links in menus, just have a look if you have the java runtime env.

Nice tutorial: http://wrapper.tanukisoftware.com/doc/english/qna-service.html

Here it has some other possibilities, using Dedicated Batch File, Command Based Batch File or Standalone Binary : http://wrapper.tanukisoftware.com/doc/english/launch-win.html#dedicated

Bruno Franco
  • 2,028
  • 11
  • 20
  • I verified that this will be just a windows app for now. So the question remains, how do I create a single executable file that a user just clicks on and the Java Gui pops up with elevated privileges? Any good tutorial links would be appreciated. Thanks! – shoota Aug 22 '14 at 13:16
0

Think you can do this with .bat file. Make sure you have java runtime env, so that you can execute jar file using java -jar command.

user2170172
  • 81
  • 2
  • 10
0

If your looking to force the user to use elevated permissions then pure Java isn't going to cut it. I suggest you write native code and use the Java Native Interface (JNI)

DeathByTensors
  • 901
  • 1
  • 10
  • 16