在启动时运行Jar文件? [英] Run Jar file on startup?

查看:131
本文介绍了在启动时运行Jar文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



当我尝试将其添加到注册表中<$使用 c $ c> [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文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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