• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Java application as .exe and running as service

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I created a simple socket server programme in java. My intention is to run this program as service on remote machines. This program will listen to all the requests coming into a designated port and then gives responses.
this is what i did
1) compiled and created a .class file.
2) used Launch4j to convert the .class to .exe.
3) used a batch file on to create a service. sc create <service name> binPath =<path of the .exe file>

Now here is problem. the service gets created. but when i try to start the service. it says that the program did not respond in timely fashion. and the service doesnt start. but in reality. it actually executes the .exe file. i confirmed it by checking the port number which it should listen and the port is occupied. i also confirmed by using a socket client program and send a message. the program responded perfectly.

so in summary. windows says that it could not start the service which it actually did. Now i think the problem is because windows is not able to comprehend the .exe file and hence although it is running it is not getting the desired exit code to make sure the service has started. what is the solution for this


1) can i have some other tool which can convert my .class to .exe
2) IS there any other way to do this. I do not want to use Java service Wrapper. my intention is to give my users, a small package. which will help them to run the socket server program as service with minimum effort. With JSW alot of config changes has to be made to convert a java app into a service.
3) Any other suggestions.


The whole objective is, i have a socket server program which I need to run it as a service on remote machines. After that i will pass some basic laundry commands from socket client residing on my server, to run on these machines and fetch the output.

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that windows service is not a simple EXE file. It is a special EXE file, one which implements an interface (a callback function actually) through which the service - or multiple services, as one EXE file can provide several of them - are controlled. Your EXE file is probably perfectly valid as an application, but since it doesn't implement the callback function, it cannot be directly set up as a windows service.

There are several applications that can turn any EXE file (or perhaps any command line) into a service. If you think this would be useful to you, I'd suggest trying to google it up - run as windows service might be a promising phrase to search on.

Another possibility would be to use Windows task scheduler - it might be nearly as good as a service without the need to create one. But obviously, some amount of setup will always be needed to run an application on a remote server.
 
Bartender
Posts: 1381
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a few experience in writing windows services using C# and .NET framework, so I can't say I'm a great expert, moreover I've never tried to create a service from a Java class. Anyway, here are my two cents.

When developing a .NET service, you have to override the onStart() method of the ServiceBase class. Now in onStart method, you have to spawn a thread which executes service's business logic, and keep a reference to it: you will use that reference to stop the thread in onStop() method.

I don't know how sc create command works, but on the base of the error you posted, I would say that the problem is that the equivalent of onStart() method doesn't terminate (or, better: it doesn't return control to SO) in a reasonable quantum of time. I don't know even how Launch4J works, I can only suppose that it uses main() method as entry code. You should try to review your main method and verify if you can start a daemon thread there.

Hope this helps.
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of converting the class file to exe why not just create a batch (bat) file and say "java MyServer" and the windows service point to the batch file.

 
Ashwin Raghavan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

K. Tsang wrote:Instead of converting the class file to exe why not just create a batch (bat) file and say "java MyServer" and the windows service point to the batch file.



I tried that. but apparently you cannot point a serivce to a batch file..

Martin Vajsar wrote:
Another possibility would be to use Windows task scheduler - it might be nearly as good as a service without the need to create one. But obviously, some amount of setup will always be needed to run an application on a remote server


The thing is i want the program to run automatically when system reboot happens. so i thought making it into a service is the best way to where in we can just make start = auto.


Claude Moore wrote:

I don't know how sc create command works, but on the base of the error you posted, I would say that the problem is that the equivalent of onStart() method doesn't terminate (or, better: it doesn't return control to SO) in a reasonable quantum of time. I don't know even how Launch4J works, I can only suppose that it uses main() method as entry code. You should try to review your main method and verify if you can start a daemon thread there.


SC is to perform operations on windows services. I think what you are pointing is correct. There is not call back method which returns the control back. My socket server does have a main method. I can make the main method return some output. right now it sends void. Can you tell me the exit codes which is returned back when the service is created.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Raghavan wrote:

Martin Vajsar wrote:
Another possibility would be to use Windows task scheduler - it might be nearly as good as a service without the need to create one. But obviously, some amount of setup will always be needed to run an application on a remote server


The thing is i want the program to run automatically when system reboot happens. so i thought making it into a service is the best way to where in we can just make start = auto.


At least in Windows 7 the task scheduler allows to schedule tasks on Windows startup. I believe this option might be available as early as Windows XP.

Claude Moore wrote:
I don't know how sc create command works, but on the base of the error you posted, I would say that the problem is that the equivalent of onStart() method doesn't terminate (or, better: it doesn't return control to SO) in a reasonable quantum of time. I don't know even how Launch4J works, I can only suppose that it uses main() method as entry code. You should try to review your main method and verify if you can start a daemon thread there.


.NET is different from Java. Since it is created by Microsoft, I believe (but might be also wrong) that .NET has built-in support for Windows services, whereas Java doesn't. There are Java frameworks that add this support, but the OP has already stated he doesn't want to use them.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ashwin Raghavan wrote:I can make the main method return some output. right now it sends void. Can you tell me the exit codes which is returned back when the service is created.


Firstly, you cannot. If you modify main to return something (not void), Java won't recognize it as the main method. Secondly, the service interface is a bit more complicated than simply returning exit codes; the callback function can be called multiple times during the life of the service, but the main method cannot.

In my opinion, you have these options (sorted by difficulty):

1. Windows Task scheduler (see above).

2. An application (not Java specific) which can create a service from any executable (I've mentioned this in my first post).

3. A Java framework to create Windows services from Java - you already know about at least one. I'd suggest to investigate further, as there is certainly more than one and some might be easier to use than others.
 
Sheriff
Posts: 28346
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And you appear to be looking for

4. Hack together a quick way to make a Java application run as a service.

I don't think that you (or anybody else) can do that. Not quickly, anyway -- if you wanted to spend a few months you might be able to get it done. That's why people tend to use the existing methods, where other people have spent a lot of time covering off all of the ugly details which are necessary.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many many years ago, I used a Java Service Wrapper. A quick google yielded this... http://wrapper.tanukisoftware.com/doc/english/introduction.html, Back then, it was open source software, it worked very well, and it basically turned a Java application into a Windows service. I haven't used it recently, so don't know what has changed. Or if it still works with the latest version of Windows.

Henry
 
Claude Moore
Bartender
Posts: 1381
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


.NET is different from Java. Since it is created by Microsoft, I believe (but might be also wrong) that .NET has built-in support for Windows services, whereas Java doesn't. There are Java frameworks that add this support, but the OP has already stated he doesn't want to use them.



No, you're right. .NET frameworks provides you with all you need to write a Windows service.
By the way: which Java frameworks allow you to write OS services ? I'd like to know more about.
 
Martin Vashko
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claude Moore wrote:By the way: which Java frameworks allow you to write OS services ? I'd like to know more about.


Henry has already given one link above. Googling for java windows service wrapper yields at least one alternative. I don't have any experience with either of them, I've only written a Windows service in plain old C many years ago.

By the way, running an application as a Windows service means it operates in a different security context, and it might bring some serious challenges - especially if the service needs to access some network resources. If it only needs to access local resources, configuring it as System local usually gives it the access to anything, but desktop interaction is still troublesome (generally requires a separate GUI app for that).
 
Claude Moore
Bartender
Posts: 1381
39
IBM DB2 Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, I'll give them a look but I'm afraid that due to architecture-neutral nature of Java I will hardly find something more than a workaround...
 
It's a tiny ad only because the water is so cold.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic