I have a Java Spring Boot application which I would like to run as a service, ideally in Mac, Windows and Linux. It should run 24/7 and be robust i.e. restart itself if it crashes.
Does anyone know the current best practice way of achieving this?
In order to make an app as a service it should confront to some Interface defined by Microsoft. Here it is:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms685942(v=vs.85).aspx
This is not the easiest solution and will make you app platform dependent. There are some wrappers that make what you want possible.
Check the Java Service Wrapper
https://wrapper.tanukisoftware.com/doc/english/introduction.html
It has options to wrap the app as windows service and unix daemon. I am not sure about macOs. There are other similar wrappers too
Best practice1 is to use the best practice solution for each platform. Unfortunately, that means different things for different platforms; e.g.
on Window, use the Windows Service mechanism
on UNIX / Linux, write a service script that uses the distro's native "init" framework
on Mac OSX, you typically use something like Automator to create a ZIP file that implements the service.
Unfortunately, in the UNIX / Linux case there are a number of different init frameworks, depending on the vintage; see this Wikipedia page.
Another answer mentions the Java Service Wrapper. I don't know how well it works with different Linux "init" frameworks, but it looks like it provides a one-size-fits-all init script written in "sh". It should be usable with systemd, though you won't get the full benefits of that framework.
1 - But read James Bach's No Best Practices article.
Related
I want to replace a legacy application running on Windows server by rewriting it in Java. This application is now connecting to a bunch of Windows servers (which are unavailable for installing another services and it is impossible to change them) via Windows Impersonation API.
I have come up with two libraries JNA and Waffle. To my understanding, Waffle uses JNA, and JNA provides calls to Windows API from java code.
The thing is, I will have my java application running on a redhat server which obviously does not have win32 api. Is there a *nix binding for Impersonation API? At least I may consider writing JNI over that binding.
I think there should be a way, because Mono and Wine provide similar abstractions on different platforms.
Have you checking on Mono or Wine to see it is supports impersonation? I don't believe it supports.
I think remote impersonation is encapsulated a lot of technologies, and some of it maybe undocumented. It might be very hard task to writing the client application to support server impersonation without using Windows API. The best way should be continue running on Windows. But if you really want to move the system to run on *nix, the best way should be create a proxy between *nix and Windows. Then later you can completely running on *nix when you don't need the services that running on Windows.
All of the solutions I found on stackoverflow suggest wrappers to register java application as windows service. My requirement is totally different. Please don't suggest wrappers for the purpose. The question is very simple I have java executable and I want to register it as windows service.
Phyiscal Path
Service Properties
Unfortunately we don't have backup of previous setup that installed it as windows service at the first place. Do I need some setup program or anything like that.
Not necessarily.
It is difficult to advise you on precisely what you need to do without more information on what you actually still have; e.g. an application installer, application JAR files, wrapper scripts, etc. Alternatively, if you told us what the application was, then maybe we could give you some hints on where to get installers, etc.
However, I can tell you definitely that registering java.exe or javaw.exe directly as a Windows Service will not work. These are not the executables for your Java application. Rather they executables for as Java Virtual Machine that will run your (real) Java application.
It is so easy task in case of Visual Studio. I want same support in Eclipse or anything else.
Well Java doesn't work like that. Java compiles to platform independent bytecode files, not to platform-specific native code. Sure, there are third party tools to generate exe's. However, using them is neither necessary, or desirable:
Why is creating an .exe from a java program not recommended?
(And asking for recommendations on what tools to use to do this is off-topic.)
Finally, if you take an arbitrary Java program and turn it into an ".exe" file, it won't necessarily be immediately registerable as a Windows service. This Q&A talks about turning an ".exe" into a Windows Service.
Create Windows service from executable
However, I can't tell you if the advice given there is appropriate for an ".exe" file created from an arbitrary Java app by some unspecified 3rd-part tool.
My recommendation:
If you are starting from scratch, use a Java Service Launcher / Wrapper.
If not, talk with whoever supplied and/or installed this application in the first place.
If you can't find any information about the application and where it came from, or if the vendor has gone out of business ... you need to urgently look for an alternative.
I am working on a configuration tool that needs to work in Windows, Solaris, and Linux. The GUI for the tool will communicate with a service thread that periodically reads information from certain hardware.
I want to use a web interface for the tool, essentially serving configuration options through dynamic HTML and CSS. This will ensure that the GUI looks consistent across platforms and I can avoid the use of applets (tool should be accessible through browser).
So my question is, is there any simple way to get Java to serve web pages (similar to WSGI in Python) and listen for HTTP POST events? I would like to only use the Java standard libraries, and the tool will more-or-less only be accessed by one person at a time.
There's the com.sun.net.httpserver package in the JDK, if you are determined not to use a library, but I recommend using something like Spring MVC for the HTML/HTTP handling combined with Maven for your build scripts. There's a bit of a learning curve, but you can probably just adapt one of the tutorial applications quite quickly.
I would like to run a Java application as a service. Unfortunately, I am limited in that I can't use something like the Java Service Wrapper (which does appear to be an excellent tool).
Is there any way of running an executable JAR, as a service, without relying on external applications? I currently have the service installed, but it fails to start. This is where I am getting stuck and I haven't been able to find anything on Google other than information about the JSW.
There's an LGPL clone of the Java Service Wrapper: http://yajsw.sourceforge.net
BTW, IANAL, but I suspect that JSW people are spreading FUD, and their software can be used to service-enable commercial applications under GPL license, just like one can gzip a commercial app for redistribution. I could be completely wrong about this, though :)
Another option, Apache Commons Daemon's procrun.
See http://commons.apache.org/daemon/
A program that should run as windows service must provide certain functions that the windows service manager uses to communicate with that service.
As long as there is no JVM that implements this functions directly (and I know of none) you will need some kind of wrapper.
I have successfully used srvany for a java based windows service (Basically it allows to run any program as windows service and it works fine with java)
You can use NSSM like this:
nssm install MyService "%JAVA_HOME%\bin\java.exe" -jar "path\to\the\file.jar"
I haven't tried it (yet), but Launch4j looks like it could suit your needs.
one more option winrun4j. the license is eclipse's CPL.
The most simple way I found was RunAsService.
A co-worker recommended a tool called SC, but I did not try it.
JSmooth can do it, and it is scriptable with ant.
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:
require a minimum amount of platform-specific code.
not require the user to do anything a general computer user couldn't/wouldn't do
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?
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()
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.
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.
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..
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.
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).
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