以具有完全权限的管理员身份运行 Java 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19037339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run Java file as Administrator with full privileges
提问by Jo?o Silva
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.
我制作了一个 Java 应用程序,并在我的个人电脑和我的个人电脑上对其进行了测试,到目前为止一切顺利...... Windows Vista 32 位)它不起作用,然后我去搜索和搜索,我发现问题是权限......我安装了一个 vista 32 位和 xp 32 位的虚拟机来做一些测试,然后我我无法以完全管理员权限运行我的应用程序。
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:
有没有办法创建批处理文件或允许我以所有权限运行我的应用程序的东西?我的意思是全部,因为我需要连接到计算机的 COM 端口以从设备获取一些数据并且我需要存储一些文件,并且由于我使用的是外部库,因此我可能需要一些我不知道的权限...我试过这个,但没有任何效果:
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
但没有任何效果:\有什么想法吗?先感谢您
采纳答案by Ashium
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.
这个答案适用于那些愿意为其 jars 或 java 类提供管理权限的人。成功开发一个exe来编辑保存在admin中的文件后。受限目录,我已经为你制定了这些步骤,希望这可以帮助你: 需要理解的事情:1)jars 不会直接用特权编译,你必须用其他一些 mainfest 文件包装它们才能最终得到 exe能够在具有特权的 windows xp/vista/ 或更高版本上运行的文件。实际上,直到现在,可能的答案是,在运行之前,exe 强制用户授予管理员权限,这与以前期望用户知道如何以不友好的管理员权限运行 jar 不同。
Now the simple steps:
现在简单的步骤:
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.
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/
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.
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>
创建像 try2.jar 这样包含一些 mainifest 文件的 jar 文件——像往常一样 My1.mf。所以,jar 文件的绝对路径将是 C:\try.jar。
现在你需要下载一个软件“Launch4j”来帮助你打包jar文件。它的下载链接是:http: //sourceforge.net/projects/launch4j/files/launch4j-3/3.1.0-beta2/
现在取出 5 分钟。并观看本教程:http: //www.youtube.com/watch?v=mARUFRknTYQ ;这将告诉您 Launch4j 的基本功能。但是这里不清楚如何为您的 exe 创建清单文件。
学完这个,然后创建一个清单文件,它是一个简单的文本文件,扩展名为“.manifest”。但是这里需要注意一些事情:首先,您的 mainfest 文件必须与要创建的最终 exe 的名称相同。就我而言,我的 exe 名称应该是“Launchme.exe”,因此,我的清单文件必须命名为“Launchme.manifest”。其次,在清单文件中只需复制以下内容:
<?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.
将上述代码复制到您的清单文件中。这里第 6 行是整个问题的关键。然后保存并关闭它。
- 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.
- 现在启动 Launch4j,根据您的条件像在视频中一样填充所有教过的文本字段。在 Wrapper mainfest 列中添加此文件清单文件。然后单击“保存配置”选项,然后单击-- 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!
现在你有包含你的 jar 的 exe,它要求用户在执行之前给予管理员权限。现在用户除了点击之外什么都不知道!
回答by Vengat Maran
Run your command prompt in administrator mode. To more specific:
在管理员模式下运行命令提示符。更具体地说:
- Locate the
%windir%\system32\cmd.exe
file. - Right click and
run as administrator
privilege.
- 找到该
%windir%\system32\cmd.exe
文件。 - 右键单击和
run as administrator
特权。
回答by Ralph Ritoch
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 SuperUserApplicationand call SU.run(new MyMain(), args) from your main function as follows.
我在https://github.com/rritoch/super-user-application上的 github 上针对此问题创建了一个通用的基于 Java 的解决方案。系统使用 JNA 或 Sudo 提供跨平台的 java 应用程序(jar),以管理员权限执行。使用它的最简单方法是让您的主类扩展抽象类SuperUserApplication并从您的主函数中调用 SU.run(new MyMain(), args) ,如下所示。
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.
这个库已经在 linux 和 windows 上进行了测试,但也应该适用于 Mac/OSX。此解决方案的主要缺点是它仅在应用程序作为 jar 执行时才起作用。当执行的 jar 没有管理员权限时,它需要以管理员权限重新执行 jar。如果应用程序不是从 jar 运行的,它会抛出 InvalidStateException。
Feel free to contribute to this project.
随意为这个项目做出贡献。
回答by PHPirate
Using Gradle
使用摇篮
You could also use Gradle and the launch4j plugin. Start at step 3 of Ashium's answer, i.e. create a file myapp.manifest
with
您还可以使用 Gradle 和launch4j 插件。在开始的步骤3 Ashium的答案,即创建一个文件myapp.manifest
与
<?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>
then add to your build.gradle
然后添加到您的 build.gradle
plugins {
id 'java'
id 'edu.sc.seis.launch4j' version '2.4.3'
}
launch4j {
mainClassName = 'com.mypackage.MainClass'
icon = "$projectDir/icons/icon.ico"
manifest = "$projectDir/myapp.manifest"
}