0

I have a Java App that can update itself.

The Java App run as a non-admin at first and if you want to update, it will execute a .lnk shortcut that I've created that points to another .jar file specifically just to update (but with admin access).

The .lnk shortcut that points to the .jar updater asks for adminpermissions by using this answer here >> https://stackoverflow.com/a/30028948/1862452

After updating the main jar, I want the .jar updater to run the main .jar again but I wanted it to be a non-admin. How do I do this? It is currently running as an admin because the updater jar is currently as running admin

MDuh
  • 415
  • 1
  • 7
  • 19
  • You need to use a low level WinAPI. Basically, you have to call [CreateProcessWithTokenW](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createprocesswithtokenw) – Johannes Kuhn Sep 23 '19 at 22:44
  • yeah I was trying to avoid using JNI stuff. I'm reading about the runas command with the lowest privilege but I can't seem to run my command: **runas /trustlevel:0x20000 "'C:\\javaw.exe' -jar 'C:\\main.jar'"** I'm thinking something wrong with the spaces since it's giving me _2: The system cannot find the file specified._ – MDuh Sep 23 '19 at 23:08

1 Answers1

0

I solved my problem in a different way.

When the user wants to update the main .jar, it executes Runtime.exec() twice. The first one where it calls cmd and executes the .lnk shortcut for the updater which request admin access. The second is a watcher for the updater .jar window.

The watcher .jar file executes a windows command tasklist /fi "Windowtitle eq <JAR UPDATER WINDOW NAME>"every so often (let's say every 1s) and count the lines of the output. It has 2 states

1) Detect Updater is running (line count of output > 1)
2) Detect Updater is dead (line count of output == 1)

After the 2 states, it means that the main.jar has been updated and the updater is dead. It then calls main.jar.

Since this watcher .jar is called with a non-admin elevation, it will also call the main.jar as non-admin.

MDuh
  • 415
  • 1
  • 7
  • 19