问题描述
我正在尝试用Java编写一个函数,该函数将关闭,强制关闭,重新启动计算机并强制重新启动计算机,并且该软件应该可以在Windows,Linux和Mac上运行.
I am trying to write a function in java that will shutdown, force shutdown, restart and force restart the computer and it should work on Windows, Linux and Mac.
Windows没问题,但是由于sudo特权,我无法在Linux上运行命令以关闭计算机.因此,我当时正在考虑使用JNA关闭计算机(我知道您可以在Windows上使用JNA来执行此操作),但是我找不到在线针对Linux或Mac的示例.
Windows is not a problem, but I am unable to run commands to shutdown on Linux due to the sudo privileges. I was therefore thinking of using JNA to shut down the computer (i know you can use JNA to do this on windows), but I can't find any examples online for linux or mac.
有人可以帮我吗?将不胜感激!
Can anyone help me out? It will be much appreciated!
即使不是通过JNA,只要我能找到实现此目标的方法,它也会对我有很大帮助.
Even if it's not through JNA, it would help me a lot as long as I can find some way to accomplish this.
推荐答案
如果仅使用Java就可以简单地覆盖系统保护,那将是一个很大的安全漏洞!我很惊讶您可以在Windows上执行此操作.
If you could simply override system protections just by using Java, that would be a big security flaw! I'm surprised you can do this on Windows.
无论如何,仅当您具有对计算机的管理访问权限以将程序设置在有权关闭/重新启动的特定组中时,才能完成此操作.否则,您将无法做到这一点.
Anyway, it can only be done if you have administrative access to your machine to set the program in a certain group which has the rights to shutdown/restart. Otherwise, you can't just do that.
我认为您可以直接将这些权限授予您的JVM,但是如果其他Java程序尝试执行关机操作,那将很危险.
I think you could give directly these rights to your JVM, but that could be dangerous if other java programs try to do shutdown.
这篇关于使用JNA或类似方法在Linux和Mac中关闭并重新启动计算机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!