Skip to content

Instantly share code, notes, and snippets.

@seraphy
Forked from anonymous/LicenseManager.launch4j.xml
Last active December 10, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraphy/4480424 to your computer and use it in GitHub Desktop.
Save seraphy/4480424 to your computer and use it in GitHub Desktop.
Javaアプリケーション上でUACによる特権昇格が必要な処理を行う方法
Javaアプリケーション上でUACによる特権昇格が必要な処理を行う場合、
1) 特権が必要な処理を別のjarに切りだし、
2) launch4jなどで
"requestedExecutionLevel = highestAvailable" or (← 自分の特権を有効化)
"requestedExecutionLevel = requireAdministrator" (← 常に管理者を要求)
なマニフェストをもつexeファイルとする。
3) アプリケーション側より、それを Desktop.open()メソッドで呼び出す.
 ※ UAC昇格にはShellExecute系APIによるexeの呼び出しが必要なため。
 ※ CreateProcess系では不可
<launch4jConfig>
<dontWrapJar>false</dontWrapJar>
<headerType>gui</headerType>
<jar>dist\LicenseManager.jar</jar>
<outfile>LicenseManager.exe</outfile>
<errTitle></errTitle>
<cmdLine></cmdLine>
<chdir></chdir>
<priority>normal</priority>
<downloadUrl>http://java.com/download</downloadUrl>
<supportUrl></supportUrl>
<customProcName>true</customProcName>
<stayAlive>false</stayAlive>
<manifest>LicenseManager.manifest</manifest>
<icon></icon>
<classPath>
<mainClass>example.LicenseManagerApp</mainClass>
<cp>lib/LicenseLib.jar</cp>
</classPath>
<jre>
<path></path>
<minVersion>1.6.0</minVersion>
<maxVersion></maxVersion>
<jdkPreference>preferJre</jdkPreference>
<initialHeapSize>128</initialHeapSize>
<maxHeapSize>128</maxHeapSize>
</jre>
<versionInfo>
<fileVersion>1.0.0.0</fileVersion>
<txtFileVersion>LicenseManager</txtFileVersion>
<fileDescription>EXAMPLE LICENSE MANAGER</fileDescription>
<copyright>seraphyware</copyright>
<productVersion>1.0.0.0</productVersion>
<txtProductVersion>LicenseManager</txtProductVersion>
<productName>LICENSE MANAGER</productName>
<companyName>seraphyware</companyName>
<internalName>LicenseManager</internalName>
<originalFilename>LicenseManager.exe</originalFilename>
</versionInfo>
</launch4jConfig>
<?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>
/**
* ライセンスマネージャを起動する.<br>
*
* @throws IOException 失敗
* @throws URISyntaxException 失敗
*/
public void openLicManager() throws IOException, URISyntaxException {
// 自ディレクトリを検出する.
CodeSource cs = getClass().getProtectionDomain().getCodeSource();
URL location = cs.getLocation();
if (!"file".equals(location.getProtocol())) {
throw new IOException("unsupported protocol. " + location.toExternalForm());
}
File baseDir = new File(location.toURI().getPath());
if (baseDir.isFile()) {
// *.jarなどの場合は、その親に設定する.
baseDir = baseDir.getParentFile();
}
// ライセンスマネージャのexeまでのパス
// (自ディレクトリと同じところにexeがあるものとする.)
File licMrg = new File(baseDir, LICENSEMANAGER_EXE);
// UACの昇格のためには、ShellExecute系のAPIである必要があるため、
// Desktop#open()を用いてexeを開く.
Desktop desktop = Desktop.getDesktop();
desktop.open(licMrg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment