Java:在 Ubuntu 上询问 root 权限 [英] Java: Ask root privileges on Ubuntu

查看:71
本文介绍了Java:在 Ubuntu 上询问 root 权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何调用身份验证窗口,从类中授予当前应用程序的 root 访问权限一段时间.例如,我正在编写一些应用程序来处理分区,因此它需要具有 root 权限才能执行某些操作.

How to call the authentication window, to grant access for root for a while for current application, from a class. for example, I'm writing some app to work with partitions, so it need's root privileges to execute some things.

推荐答案

使用 Java 可能无法做到这一点.最好的方法是,也许您可​​以检测到该程序未以 sudo 运行,因此您可以要求用户关闭程序并以超级用户身份重新启动.(对于这个解决方案,我更喜欢从系统文件中选择一个文件或目录并检查它是否可写.例如 new File("/usr/bin/ls").isWritable())

Probably you can't do that with Java. The best way to do is, maybe you can detect that program is not run as sudo so you can ask users to shutdown the program and relaunch as super user. (for this solution, I'd prefer choosing a file or directory from system files and check whether it is writable or not. e.g. new File("/usr/bin/ls").isWritable())

其他方式可能是启动特定于桌面环境的身份验证窗口.例如有 Java-gnome 但正如你所料,不是所有应该使用 GNOME 的用户.因此,并没有真正的通用解决方案,而且所有解决方案都带有大量依赖项.Java 不是为此而设计的.

Other ways might be launching Desktop Environment-specific authentication windows. e.g. There is Java-gnome but as you might expect, not all users supposed to use GNOME. So there is not really a universal solution and all comes with tons of dependencies. Java is not designed for this.

最好的办法是在启动时使用技巧检查超级用户权限.也许您可以详细说明您的问题,我们可以提出不需要 root 访问权限的解决方案.

The best thing is to check super user privileges with a trick on startup. Maybe you can elaborate your problem and we can come up with a solution that does not require root access.

这篇关于Java:在 Ubuntu 上询问 root 权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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