Unable to start .jar file as a window service


  1. Posts : 2
    windows 7 professional 32bit
       #1

    Unable to start .jar file as a window service


    Hello Everyone,

    This is my 1st thread, so i'm naive about the categories under which the issue i'm facing falls. I've a jar file which contains a very simple program of displaying "Hello" message in the console.I've pasted the code at the end of this thread.I want to run this jar file as a windows service.This service gets started without any hiccup,However,It simply wouldn't start the service.

    I've tried running this service as using NET and Sc command.But in vain.Below is the error i'm facing

    C:\Windows\system32>sc start MajaService
    [SC] StartService FAILED with error 193.


    Further, as a workaround I created a .bat file for executing this .jar file and again tried creating a window service for this .bat file,In this event i get the followingh error:

    [SC] StartService FAILED 1053:

    The service did not respond to the start or control request in a timely fashion.



    and below is my program:
    -----------------------------------------------------------------------------------
    import java.io.*;


    class Hello implements Runnable
    {

    Thread thread;

    Hello()
    {
    thread = new Thread(this, "Hello1");
    thread.start();
    }


    public void run()
    {

    try
    {
    while(true)
    {
    System.out.println("Hello");
    Thread.sleep(500);
    }
    }


    catch(Exception e){}


    }


    public static void main(String[] args)
    {
    Hello h = new Hello();

    }
    }


    ---------------------
    any Sort of Help is appreciated.

    Regards,
    Supriya
      My Computer


  2. Posts : 2,468
    Windows 7 Ultimate x64
       #2

    A few things I've noted on that:

    First of all, to run a program in Windows, you need an EXE, not a jar. EXEs are the only really executable things on Windows, so I guess you need to compile your program first (I don't do any myself java, so can't help very much with it).

    Second, programs need an special "contract" to work as a service. Your program, in addition to do its normal work, needs to call and attend to some predefined Windows APIs that interact directly with the service controller, to do things such as responding to start/stop/pause events and to report its current status. Your program simply prints a string to the console regularly and fails to do such protocol, so it will never be recognized directly as a service.
    There is a workaround however. MS created a small executable called "instsrv" that allows any program to run as a service (in fact, it does all the service's API calls and thens runs your program). Here's the details on how to do it: How To Create a User-Defined Service. I've done that myself a few times on my work and works good enough.

    Most important, it seems that you're not understanding correctly how a service works. A service is meant to be a background process, something that runs all the time totally unattended without intervention or any input or output. What you want just violates that principle, because services don't show a console or any kind of window for the matter. Additionally, starting with Win Vista/2008, there is an additional problem called "session 0 isolation" that causes all the output created by services to be sent to a separate desktop that it's almost impossible to access.
    My guess is that if you want to do a test of a "hello world" every few seconds, better to write that to a file instead.

    If need any help just ask. And sorry for the loooong post
      My Computer


  3. Posts : 2
    windows 7 professional 32bit
    Thread Starter
       #3

    Hi Alejandro,

    The code I'd pasted earlier ran infinitely as the service is supposed to run...I used a tool called AlwaysUp to run my .bat file service.It worked but rather strangely....The whole screen went completely dark and then it turned grey and began many hellos .Hence As per your advice I tweaked my code a bit...rather then printing the string on console I directed the output to a text file.But now there seems to be a rather grave problem.Now when I try to access a text file where my output resides,the file simply doesnt responds...It ask me to wait....forever...also when i checked the file size it was 2.56 gb in 3 minutes and it was still growing...Do you have any workaround for this ??? Between Thanks a lot 4 replying to my thread...Your loooong post(It was precisely perfect) helped me understand where i was making mistake which i couldn't understand inspite visiting numerous websites.
    Regards.
      My Computer


  4. Posts : 2,468
    Windows 7 Ultimate x64
       #4

    Hi Supria

    Glad to have helped. This is going to be another loooooong post

    Supria1405 said:
    It worked but rather strangely....The whole screen went completely dark and then it turned grey and began many hellos
    I knew and have seen myself that behavior too, and was surprised at first but that's actually normal and the expected way it works on Win7 (since Win Vista) and is due to the so called "session 0 isolation". Long story short, services run in a separate desktop completely isolated from what the user sees, and the service's output is drawn there. When somethings happens there you have the chance to switch to that desktop (the gray background you see) and look at the program. In XP and before the console would have appeared normally.
    These two links explain it and the change very well:
    Services isolation in Session 0 of Windows Vista and Longhorn Server - Cyril Voisin (aka Voy) on security - Site Home - TechNet Blogs
    Windows Sysinternals : Windows Core Concepts - Sessions, Window Stations, Desktops, and Window Messages - Microsoft Certification Examples, exercises, practises, tutorials, solutions about Windows



    Supria1405 said:
    I directed the output to a text file.But now there seems to be a rather grave problem.Now when I try to access a text file where my output resides,the file simply doesnt responds...It ask me to wait....forever...also when i checked the file size it was 2.56 gb in 3 minutes and it was still growing
    Better idea to test services in a "hello world" way
    Don't know exactly what is happening but my guess is that the service is keeping the file open all the time and blocks it, preventing anything else from reading it. And since it writes an string every 500 milliseconds (are those milliseconds?, and assuming you've used the same code you posted) it's too frequently to give notepad time to open. Just to make sure it really works. Write every, say, 10 seconds and open/close the file each time to give programs the necessary access in between.
    The 2 GB doesn't surprise me really, considering the program is churning out more text several times a second and never deletes it until manually stopped. But for a test only, it isn't anything to worry about just yet. Real life, production code will have to take that into account and delete old entries or move then somewhere else.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 09:05.
Find Us