好的,我终于设法解决了一个我很满意的问题;这有点丑陋,但它可以满足我的需求。
我从借来的代码这个答案做实际的权限提升;
从那里开始,问题是实际上使该解决方案与Java一起工作的问题之一。最终的代码如下所示:
if (!checkPrivileges()) { try { String jarPath = DownloaderMain.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String depredPath = URLDeprer.depre(jarPath, "UTF-8"); depredPath = depredPath.substring(1, depredPath.length()); Elevator.executeAsAdministrator(System.getProperty("java.home") + "\bin\java", "-jar " + """ + depredPath + """); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else { // Run with elevated privileges }该
checkPrivileges方法从上面没有改变,并且
Elevator该类实际上与链接的解决方案中出现的类相同(我只是取出了不需要的
main方法)。该解决方案假定要提升的过程是一个罐子;可以根据自己的需要进行更改,这并不难。



