Windows上的Java:测试Java应用程序是否作为提升的进程运行(具有管理员权限) [英] Java on Windows: Test if a Java application is run as an elevated process (with Administrator privileges)

查看:204
本文介绍了Windows上的Java:测试Java应用程序是否作为提升的进程运行(具有管理员权限)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况

我有一个在多个平台上运行的(基于Eclipse RCP的)Java应用程序。我在除Windows之外的所有平台上都弄明白了。

I have an (Eclipse RCP-based) Java application running on multiple platforms. I got this figured out on all platforms except Windows.

安装程序:我的应用程序安装程序始终以提升模式运行,因此它可以安装申请 C:\Program files\MyProduct 。从用户的角度来看,这意味着安装程序只能由管理员执行,UAC会要求确认。这很好。

Installer: My application installer is always run in elevated mode, so it can install the application to C:\Program files\MyProduct. From a user perspective, this means the installer can only be executed by an Administrator and the UAC will ask for confirmation. This works fine.

正常使用:普通用户可以启动应用程序 。不需要管理员权限。这很好。

Normal usage: The application can be launched by normal users. No administrator privileges should be required. This works fine.

自动更新自动更新功能也会写入 C:\Program Files \ MyMyProduct 因此还需要管理员权限。这就是为什么应用程序虽然可以作为普通应用程序启动,但必须作为升级过程运行以自动更新。从用户的角度来看,它应该是以管理员身份运行来自动更新。

Auto-update: The auto-update functionality also writes to C:\Program Files\MyProduct and therefore also requires administrator privileges. That's why the application, while it can be launched as a normal application too, MUST be run as an elevated process to auto-update. From a user perspective, it should be 'Run as administrator' to auto-update.

问题

我想要一个运行时检查来查看我的 Java进程是否处于提升模式 (即查看它是否是'以管理员身份运行'。

I would like a runtime check to see if my Java process is in elevated mode (i.e. to see if it was 'Run as administrator'.

注意它可能是仅限Windows的解决方案。使用Java反射API ,我可以在运行时检查Windows和/或特定于实现的类。

Note it may be a Windows-only solution. Using the Java reflection API, I can check for the Windows- and/or implementation-specific classes at runtime.

研究

我只在StackOverflow上发现了这个问题:
检测Java应用程序是否作为Windows管理员运行

I only found this question on StackOverflow: Detect if Java application was run as a Windows admin

但是,该解决方案将返回活动用户是否为管理员的成员用户可能仍然以非提升模式启动我的应用程序。我已经验证了这一点。

However, that solution returns whether the active user is a member of the Administrator group. The user may still have my application launched in non-elevated mode. I have verified this.

注意

我知道当他或她没有管理员权限时,Eclipse RCP应用程序会自动在用户目录中安装更新,但是我想阻止这种情况。

I know that an Eclipse RCP application will automatically install updates in the user directory, when he or she has no administrator privileges, but I want to block that case.

我想允许特定于用户的配置(工作正常),但允许用户特定的更新会在卸载后留下太多混乱。

I want to allow user-specific configuration (which works fine), but allowing user-specific updates would leave too much mess after uninstallation.

推荐答案

这是Eclipse LocationManager 确定它是否可以写入安装目录:

This is what the Eclipse LocationManager does to determine if it can write to the install directory:

public static boolean canWrite(File installDir) {
    if (installDir.canWrite() == false)
        return false;

    if (!installDir.isDirectory())
        return false;

    File fileTest = null;
    try {
        // we use the .dll suffix to properly test on Vista virtual directories
        // on Vista you are not allowed to write executable files on virtual directories like "Program Files"
        fileTest = File.createTempFile("writtableArea", ".dll", installDir);
    } catch (IOException e) {
        //If an exception occured while trying to create the file, it means that it is not writable
        return false;
    } finally {
        if (fileTest != null)
            fileTest.delete();
    }
    return true;
}

注意尝试创建一个dll

Note the attempt to create a dll

这篇关于Windows上的Java:测试Java应用程序是否作为提升的进程运行(具有管理员权限)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆