在启动时运行Jar文件?

编程入门 行业动态 更新时间:2024-10-26 18:19:24
本文介绍了在启动时运行Jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Java应用程序,它存在于我编译为可执行jar文件的系统托盘中。我想在我的程序中添加选项以添加到系统启动项。

因为我不知道为所有操作系统做任何统一的方法我假设我必须为每个我打算支持的代码编写代码,所以我从Windows开始。

当我尝试将其添加到注册表中 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] 这里提供的代码我发现在Windows 7和8下,除非我具有管理员权限(通过从提升的命令提示符运行),我对注册表的编辑不适用。

然后我花了一天的时间试图弄清楚如何让Jar重新启动自己的管理员权限,然后我就放弃了这个hacky解决方法。

可以任务我想实现甚至完成如果是这样的话?

解决方案

在大多数情况下,您实际上是要添加自动启动用户登录的功能,而不是系统启动时。对于Windows,如果在以下位置添加注册表项:

HKEY_CURRENT_USER \Software\Microsoft \ Windows \ CurrentVersion \运行

此位置在以普通用户身份运行时不会受到权限问题的影响,并且已受到支持在Windows下很长一段时间(我在这里考虑Windows 95时间框架),因此应该对所有系统进行安全更改。

对于Linux,假设操作系统遵循 Open Desktop AutoStart 规范,然后您需要创建 $ HOME / .config / autostart / 中的相应 .desktop 文件,在这种情况下它应该在登录时自动启动。

对于Mac OS X,您需要在 $ HOME / Library / LaunchAgents 中创建启动代理plist。 守护进程和服务文档详细说明了构造这个文件。

I have a Java application that lives in the system tray that I compile to a executable jar file. I would like to add the option within my program to add to the system startup items.

As I do not know of any uniform way to do this for all operating systems I assumed I would have to write code to do it for each one I intend to support so I started with Windows.

When I attempted to add it to the registry at [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] using the code available here I discovered that under Windows 7 and 8 unless I have administrator privileges (by running from an elevated command prompt) my edits to the registry do not apply.

Then I spent a day trying to figure out how to get the Jar to relaunch itself with admin privileges before I gave up on that hacky workaround.

Can the task I'm trying to achieve even be accomplished and if so how?

解决方案

For the most part, you're actually looking to add the feature of auto starting on user login, rather than on system startup. For windows, if you add the registry entry under:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run

This location does not suffer from permissions issues when run as an ordinary user, and has been supported for a long time under Windows (I'm thinking Windows 95 time frame here), so should be a safe change across all systems.

For Linux, assuming that the operating system is following the Open Desktop AutoStart specification, then you need to create the appropriate .desktop file in $HOME/.config/autostart/ and it should autostart on login in that case.

For Mac OS X, you need to create a launch agent plist in $HOME/Library/LaunchAgents. The Daemons and services documentation details how to construct this file.

更多推荐

在启动时运行Jar文件?

本文发布于:2023-11-05 06:20:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560132.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:启动时   文件   Jar

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!