【问题标题】:Request admin privileges for Java app on Windows Vista在 Windows Vista 上请求 Java 应用程序的管理员权限
【发布时间】:2010-09-20 11:58:41
【问题描述】:

当我尝试通过 Java ProcessBuilder 类在任务调度程序中创建新任务时,我收到 Windows Vista 的拒绝访问错误。在 XP 上它工作得很好。

当我使用“以管理员身份运行”选项时,它也可以在 Vista 上运行..

但是,这是一个额外的步骤,用户可能不知道。当用户只是双击应用程序图标时,它将失败并拒绝访问。我的问题是如何强制 Java 应用在启动后立即重新使用管理员权限?

【问题讨论】:

    标签: java windows-vista scheduler access-denied


    【解决方案1】:

    您是否考虑过使用 launch4j 将您的 Java 应用程序封装在一个 .exe 中?通过这样做,您可以嵌入一个清单文件,该文件允许您为可执行文件指定“执行级别”。换句话说,您可以控制应该授予正在运行的应用程序的权限,从而有效地告诉操作系统“以管理员身份运行”,而无需用户手动执行此操作。

    例如...

    build.xml:

    <target name="wrapMyApp" depends="myapp.jar">
            <launch4j configFile="myappConfig.xml" />
    </target>
    

    myappConfig.xml:

    <launch4jConfig>
      <dontWrapJar>false</dontWrapJar>
      <headerType>gui</headerType>
      <jar>bin\myapp.jar</jar>
      <outfile>bin\myapp.exe</outfile>
      <priority>normal</priority>
      <downloadUrl>http://java.com/download</downloadUrl>
      <customProcName>true</customProcName>
      <stayAlive>false</stayAlive>
      <manifest>myapp.manifest</manifest>
      <jre>
        <path></path>
        <minVersion>1.5.0</minVersion>
        <maxVersion></maxVersion>
        <jdkPreference>preferJre</jdkPreference>
      </jre>
    </launch4jConfig>
    

    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>  
    

    查看http://msdn.microsoft.com/en-us/library/bb756929.aspx和launch4j网站了解模式详情。

    【讨论】:

      【解决方案2】:

      我不确定您是否可以通过编程方式进行操作。如果您的应用有安装程序,您可以添加注册表项以强制以管理员身份运行:

      路径: HKEY_CURRENT_USER\软件\微软\Windows NT\Currentversion\Appcompatflags\layers

      键:>

      值:RUNASADMIN

      类型:REG_SZ

      【讨论】:

      • 我不确定是否要在 javaw.exe 上设置该标志!
      • 您不需要管理员权限来编辑注册表
      【解决方案3】:

      虽然 FreeCouch 的回答很好,但我遇到了一些问题,想把它放在这里,以便下次其他人可以轻松受益。

      要在 Java 中获得管理员权限,据我所知,解决方案是将 *.jar 包装在带有 Launch4J 之类的程序的 *.exe 中,并将 Manifest 与它绑定。

      要创建 Manifest 文件,请打开任何文本编辑器(如记事本)并将这些行粘贴到其中,并使用文件名及其扩展名保存它,例如:myApplication.exe.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>  
      

      在 Launch4J 中,在 Basic 选项卡上将此文件添加到 Wrapper Manifest 选项中。

      【讨论】:

        【解决方案4】:

        我还没有找到最好的解决方案。但与“James Van Huis”相比,我有另一种解决方法。请改用 RUNASINVOKE,这样您就不必在每次运行应用程序时都看到提示 Allow/Deny。

        注意:您必须始终是管理员,这是我要解决的问题

        【讨论】:

          【解决方案5】:

          使用 launch4j Gradle 插件

          尤其是在使用 Gradle 时,很容易应用 launch4j plugin 来解决此问题。

          freecouch' answer一样,创建一个文件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> 
          

          然后通过添加到您的 build.gradle 来应用 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"
          }
          

          或者,如果使用 Kotlin Gradle DSL,请发送至您的 build.gradle.kts

          plugins {
              application
              kotlin("jvm") version "1.2.31"
              java
              id("edu.sc.seis.launch4j") version "2.4.3"
          }
          
          launch4j {
              mainClassName = "com.mypackage.MainKt"
              icon = "$projectDir/icons/icon.ico"
              manifest = "$projectDir/myapp.manifest"
          }
          

          【讨论】:

            猜你喜欢
            • 2011-09-03
            • 2021-07-27
            • 2013-07-20
            • 1970-01-01
            • 2018-05-01
            • 2014-02-08
            • 2020-07-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多