2

I'm currently developing a study tool which groups several portable system management tools (mainly sysinternal tools). I have a simple frame with a JButton.

What am I trying to do? - Along with my java file i have an exe file (for example purposes let's use config.exe) which needs elevated rights to run.

After the user clicks on the button how can i do this execute this file?

EDIT: I just found one other way to do it. I made a exe from my jar file and went to the compatibility tab and checked "Always Run as admin" Thank you for all of your help.

Ângelo
  • 71
  • 2
  • 3
  • 12

2 Answers2

5

First of all locate the directory in which the exe file is located.Then create a text file named as

"Your_Exe_File_Name".exe.manifest

Just put the below contents to the file and save it.

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <assemblyIdentity version="1.1.1.1"
   processorArchitecture="X86"
   name="MyApp.exe"
   type="win32"/>
  <description>elevate execution level</description>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
  <security>
   <requestedPrivileges>
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
   </requestedPrivileges>
  </security>
  </trustInfo>
 </assembly>

Now use this in your java code to invoke the exe.It will be automatically invoked with Admin Rights.

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2",).start();
InputStream is = process.getInputStream();//Get an inputstream from the process which is being executed
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);//Prints all the outputs.Which is coming from the executed Process
}

I think it will be helpful for you.

ShihabSoft
  • 835
  • 6
  • 15
  • Hello, thank you for such usefull information. I am using Launch4j to add the manifest to the exe but after compiling the exe i always get the error "The application has failed to start because its side-by-side configuraton is incorrect." how can i overcome this? Thx – Ângelo Apr 15 '14 at 13:06
  • OK,Before embedding the manifest into exe.Have you ran it.What is the result? – ShihabSoft Apr 15 '14 at 14:46
  • Yes i can run it but i can't execute the exe. I'm having a weird bug regarding the privileges. My program has two main functionalities, changing the network interfaces IP addresses and opening other programs but right now, if i right-click on the exe and "Execute as administrator" i can change the network interface ip but can't run the exes. If i just execute the jar file i can ran the exes but can't change the ip addresses. Any hint on this ? – Ângelo Apr 15 '14 at 15:16
  • What do yo u mean **i can change the network interface ip but can't run the exes** – ShihabSoft Apr 15 '14 at 15:24
  • As for the EXEs i mean the other applications that are inside a folder called "Applications" These are portable applications that my java program refers to and it's able to execute them (Some of them need administration rights) and they're mainly Sysinternals tools. As for changing the network interfaces, if you have one NIC with DHCP enable and want to change it through my program you're also able to do it. This is beeing made by referring to cmd commands. – Ângelo Apr 15 '14 at 15:27
  • I think you have now solved the administration right problems when executing exe.No errors such as "**SIDE BY SIDE CONFIGURATION ERROR**".If the error persists.Just see my answer above.I have edited the content to be produced into the manifest file.It will solve the problem of Admin RIghts. – ShihabSoft Apr 15 '14 at 15:36
1

Seeing as you're trying to run an exe file, I'll assume this is Windows.

The standard way of executing external commands in java is the .exec command:

Runtime.getRuntime().exec("path\to\config.exe");

Now, to get config.exe to run as admin, what you need to do is set it to run as admin from Windows. Right click the file in Explorer, and select Properties. Select the Compatibility tab and check Run this program as administrator near the bottom. Now, whenever the program is run, it will ask for elevated privileges before running.

astine
  • 276
  • 2
  • 9