java background/daemon/service cross platform best practices
Asked Answered
T

7

27

I am looking for the best way to make my desktop java program run in the background (daemon/service?) across most platforms (Windows, Mac OS, Linux [Ubuntu in particular]).

By "best way" I am hoping to find a way that will:

  1. require a minimum amount of platform-specific code.
  2. not require the user to do anything a general computer user couldn't/wouldn't do
  3. not be a resource hog.

I understand that my requirements may be unrealistic but I am hoping there is some sort of "best practice" for this type of situation.

How to go forward?

Timberhead answered 28/11, 2008 at 19:38 Comment(2)
which is it - a desktop program, or a daemon/service? It can't really be both.Virtues
Ideally, the program would mainly be running as a daemon/service...and clicking on its icon in the status bar would show a window to change settings,stop the daemon/service, etc...Currently, the app is only a swing desktop app.Timberhead
A
7

You can use the SystemTray classes and install your app as any other in the default platform.

For windows it could be an scheduled task that run at startup. For Linux and OSX I don't know (besides crontab wich is somehow too technical) but I'm pretty sure they both have a way to do the same thing easily.

Unfortunately (as of today) Apple hasn't finished the 1.6 port.

It won't be a real demon, but an app like Google Desktop.

I've heard Quartz is a good option. But I've never used it.

Apfel answered 28/11, 2008 at 19:54 Comment(0)
P
16

You can run a Java application as a service (Windows) or daemon (Linux) using the Apache Commons daemon code.

Structure

Daemon is made of 2 parts. One written in C that makes the interface to the operating system and the other in Java that provides the Daemon API.

Platforms

Both Win32 and UNIX like platforms are supported. For Win32 platforms use procrun. For UNIX like platforms use jsvc.

Java code

You have to write a Class (MyClass) that implements the following methods:

* void load(String[] arguments): Here open the configuration files, create the trace file, create the ServerSockets, the Threads
* void start(): Start the Thread, accept incoming connections
* void stop(): Inform the Thread to live the run(), close the ServerSockets
* void destroy(): Destroy any object created in init()
Polito answered 29/12, 2008 at 22:45 Comment(0)
W
9

You can turn any Java program into a service/daemon using the Java Service Wrapper. It is used by multiple OSS projects, and ships as part of the Nexus Maven Repository Manager so that it can be installed as a service out of the box. To use it, you, the author, just need to create a configuration file and then run a simple batch file to create the service on Windows or copy an init script to the correct runlevel on Linux.

Whitlow answered 28/11, 2008 at 20:28 Comment(0)
A
7

You can use the SystemTray classes and install your app as any other in the default platform.

For windows it could be an scheduled task that run at startup. For Linux and OSX I don't know (besides crontab wich is somehow too technical) but I'm pretty sure they both have a way to do the same thing easily.

Unfortunately (as of today) Apple hasn't finished the 1.6 port.

It won't be a real demon, but an app like Google Desktop.

I've heard Quartz is a good option. But I've never used it.

Apfel answered 28/11, 2008 at 19:54 Comment(0)
M
1

If you dont need free solution, you can use Advanced Installer (www.advancedinstaller.com), it can make win-service as well as MacOS installer from your JAR, and more..

Medicinal answered 13/8, 2011 at 11:37 Comment(0)
M
0

Check out JDIC, the Java Desktop Integration Components project. It supports desktop integration like system tray (or equivalent) with a cross-platform API.

Others have mentioned Quartz, which is an enterprise job scheduler. It can be lightweight, depending on the jobs that are scheduled, but it doesn't have any features that are inherently desktop-oriented. On the contrary, many of its features depend on enterprise support like a relational database. If your application is primarily scheduling tasks, a headless Quartz service executing jobs, with a desktop client to interact with the service is reasonable approach.

Mf answered 28/11, 2008 at 20:4 Comment(0)
U
0

The Apache Directory Daemon project sounds like the best cross platform way to do this (with Java wrappers for JSVC under POSIX and procrun under windows).

Ubangishari answered 31/12, 2010 at 13:28 Comment(0)
M
0

People sometimes have difficulties finding prunsrv.exe and/or prunmgr.exe (components of procrun), its not well documented on the apache site, generally it can be found in the archives (note. they say that procrun is tomcat5.exe)

for windows you can find it here: http://archive.apache.org/dist/commons/daemon/binaries/windows/commons-daemon-1.0.7-bin-windows.zip (contains both 32 and 64 bit versions)

you dont need to implement daemon interface nor download it at all, prunsrv can be used to turn any app to windows service

browse that archive for other platforms

Medicinal answered 15/8, 2011 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.