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

当我使用<!>时;以管理员身份运行<!>;选项它也可以在Vista上运行..

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

有帮助吗?

解决方案

我不确定你能以编程方式做到这一点。如果您有应用程序的安装程序,则可以添加注册表项以强制运行为admin:

  

路径:   HKEY_CURRENT_USER \ SOFTWARE \微软\的Windows   NT \ CURRENTVERSION \ Appcompatflags \层

     

键:<!> lt; <!> lt; exe的完整路径<!> gt; <!> gt;

     

价值:RUNASADMIN

     

输入:REG_SZ

其他提示

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

例如......

的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网站了解模式详情。

虽然FreeCouch的答案很好,但我遇到了一些问题,并考虑将其放在这里,以便下次其他人可以轻松受益。

要获得Java中的管理员权限,据我所知,解决方案是使用Launch4J等程序将* .jar包装在* .exe中,并使用它绑定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选项中。

我还没有找到最好的解决方案。但是,我有一个替代方法,与<!>“James Van Huis <!>”相比较。请改用RUNASINVOKE,这样您就不必在每次运行应用程序时都看到提示允许/拒绝。

注意:你总是必须是管理员,我想解决的问题

使用launch4j Gradle插件

特别是在使用Gradle时,很容易应用 launch4j插件来修复此问题

freecouch'答案一样,使用

创建文件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"
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top