Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Java file as Administrator with full privileges

Tags:

I have made a Java Application and I tested it in my pc and my coined pc, so far so good... But this application is for another friend of mine that have a disco, when I "installed" my application on his pc (Windows Vista 32 bits) it didn't work, then i go searching and searching and I find out that the problem as the privileges... I installed a virtual machine of vista 32 bits and xp 32 bits to do some tests and I'm not able to run my application with full administrator privileges.

Is there any way to create a batch file or something that would allow me to run my application with all privileges? I mean all because I need to connect to a COM port of the computer to get some data from a device and I need to store some files, and since I'm using an external library I might need some privileges that I don't know... I have tried this and nothing works:

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

using the elevate scripts from microsoft, I also created a batch file with

runas /user:Administrator myjar.jar 

but nothing worked :\ Any ideas? thank you in advance

like image 525
João Silva Avatar asked Sep 26 '13 19:09

João Silva


People also ask

How do I run a program with elevated privileges?

When using the Start Menu, hold down Shift+Ctrl when launching an application to launch it 'As Admin' (elevated) this has the same effect as if you right click and select Run as local Administrator. To run an item on the Start menu / Search /Desktop elevated, select it and then press Ctrl+Shift+Enter on the keyboard.

How do I get full administrator permission?

How Do I Get Full Administrator Privileges On Windows 10? Search settings, then open the Settings App. Then, click Accounts -> Family & other users. Finally, click your user name and click Change account type – then, on the Account type drop-down, select Administrators and click OK.

How do I run a batch file as administrator in Java?

Under the Shortcut tab, click the Advanced... button. Check the Run as administrator checkbox and press OK to both the modal window and the main properties window. Run the shortcut by double-clicking it and the batch file should run as administrator.


2 Answers

This answer is for those who are wiling to provide administrative privileges to their jars or java classes. After successfully developing an exe to edit files kept in admin. restricted directories, I have developed these steps to follow for you, hope this may help you: Things to understand: 1) Jars won't be directly compiled with privileges rather, you have to wrap them with some other mainfest file to finally have exe files capable of running on windows xp/ vista/ or higher version with privileges. Actually the possible answer uptil now is that before running, exe forces user to give admin rights unlike before where user is expected to know how to run jar with admin rights which isn't friendly.

Now the simple steps:

  1. Create your jar file like try2.jar containing some mainifest file-- My1.mf as like always.So, the absolute path of the jar file would be C:\try.jar.

  2. Now you need to download a software "Launch4j" which will help you to wrap jar files.Its download links is : http://sourceforge.net/projects/launch4j/files/launch4j-3/3.1.0-beta2/

  3. Now take out 5 min. and watch this tutorial: http://www.youtube.com/watch?v=mARUFRknTYQ ;this will tell you basic functions of Launch4j. But here it won't be clear how to create a manifest file for your exe.

  4. After learning this, then create a manifest file, it is a simple text file with extension ".manifest" saved. But here certain things to be taken care of: Firstly, your mainfest file has to have same name as that of your final exe to be created. In my case, name of my exe was supposed to be "Launchme.exe" thus, my manifest file had to be named as "Launchme.manifest". Secondly, in manifest file just copy this content:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges> <requestedExecutionLevel level="highestAvailable"   uiAccess="False" /> </requestedPrivileges> </security> </trustInfo> </assembly>   

copy the above code into your manifest file. here line 6 is the key of whole issue. Then save it and close it.

  1. Now start Launch4j, fill all the taught textfields as like in video as per your conditions. In Wrapper mainfest column add this file manifest file. Then click "save configuration" option and then click-- Build Wrapper.

Now you have exe containing your jar which requests user to give admin rights before executing. Now user is free from knowing anything other than clicks!

like image 106
Ashium Avatar answered Sep 30 '22 19:09

Ashium


I have created a generic Java based solution to this issue on github at https://github.com/rritoch/super-user-application . The system uses JNA or Sudo to provide a cross platform java application (jar) that executes with administrator rights. The easiest way to use it is to have your main class extend the abstract class SuperUserApplication and call SU.run(new MyMain(), args) from your main function as follows.

package com.vnetpublishing.java;  import com.vnetpublishing.java.suapp.SU; import com.vnetpublishing.java.suapp.SuperUserApplication;  public class TestAdmin extends SuperUserApplication {      public static void main(String[] args) {         SU.run(new TestAdmin(), args);     }      public int run(String[] args) {         System.out.println("RUN AS ADMIN! YAY!");         try {             Thread.sleep(5000);         } catch (InterruptedException e) {             e.printStackTrace();         }         return 0;     } } 

This library has been tested on linux and windows, but should also work for Mac/OSX. The main drawback with this solution is that it only functions when the application is executed as a jar. When the executed jar doesn't have administrator rights it needs to re-execute the jar with administrator privileges. If the application was not run from a jar it throws an InvalidStateException.

Feel free to contribute to this project.

like image 44
Ralph Ritoch Avatar answered Sep 30 '22 19:09

Ralph Ritoch