5

Request:

This is a very common problem faced by Java devs in my locale. I am really stuck for many days on this. Searched and tried a lot, read the docs. read ALL the stackoverflow questions related to JavaExe. Please only reply if you have done similar thing before and have a comprehensive answer. I would be really grateful to the community!

Senario:

I am using JavaExe to run an application as system service in desktop interactive capability. To be exact I have an application that captures screenshots of desktops. I want it to run (as admin) on any user login so no one can stop it.

I have a myapp.jar, settings.txt and a lib dir.

I have searched alot and found JavaExe works (by watching its examples)

If anyone has a better way. Please state so.

Problem:

According to my research,

  1. you must create a .properties file that has named like the .exe, and write "RunType = 1" in this file.

  2. you define a static method in your main class : serviceInit()

Do I need to place any class or reference/import? How?

Edit:

My code below works as stand alone .jar and in javaExe.exe too.

It now does makes a system service and runs by as SYSTEM user. but It is NOT interactive to desktop. i.e its not showing any GUI.

package temp;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


public class Temp {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


          serviceInit();

    }

    public static boolean serviceInit(){

        new Thread(){
            public void run(){
                Integer i = 0;
                while(i < 999999999){
                    JOptionPane.showMessageDialog(null,i);
                    i++;

                }
            }
        }.start();

        return true;
   }




}

And I dont think that bundling the .jar, lib directory and settings.txt into one .exe is possible?

tckmn
  • 57,719
  • 27
  • 114
  • 156
Masood Ahmad
  • 731
  • 4
  • 15
  • 38

2 Answers2

2

you should have in your case :

public class MyApp_ServiceManagement
{
    static boolean isMsgToDisplay = false;

    /////////////////////////////
    public static boolean serviceInit()
    {
        (new Thread()
        {
            public void run()
            {
                for(int i=0;i < 6;i++)
                {
                     try { sleep(5*1000); }
                     catch(Exception ex) {}

                     isMsgToDisplay = true;
                }
            }
        }).start();

        return true;
    }

    /// is Data ready to be send to the UI ?
    public static boolean serviceIsDataForUI()
    {
        return isMsgToDisplay;
    }

    /// Data to be send to the UI
    public static Serializable serviceDataForUI()
    {
        isMsgToDisplay = false;
        return "hello, I am an interactive Service";
    }
}

and for the UI part :

public class MyApp_TaskbarManagement
{
    /// To show (or not) the icon in tray
    public static boolean taskIsShow()
    {
        return false;
    }

    /// Receive the message from Service
    public static void taskDataFromService(Serializable data)
    {
        JOptionPane.showMessageDialog(null, data);
    }

    /// descr of UI
    public static String[] taskGetInfo()
    {
        return new String[]
            {
                "UI part of Service"
            };
    }
}
bb67
  • 160
  • 3
  • 10
  • And I dont think that bundling the .jar, lib directory and settings.txt into one .exe is possible? – Masood Ahmad May 29 '13 at 20:24
  • Please see the edit code now that works but not interactive to desktop. – Masood Ahmad May 29 '13 at 22:41
  • the JOptionPane.showMessageDialog can't be called from service, only from TaskbarManagement – bb67 May 29 '13 at 23:18
  • AND how can I do things with Robot.class of java. moust and keyboard and screen events and movement/ capuring – Masood Ahmad May 29 '13 at 23:31
  • @djangofan great. I hope no non-admin could stop the service. – Masood Ahmad May 30 '13 at 00:07
  • @bb67 Thanks . fantastic. I wish I could accept both answers of yours but stack permits one. – Masood Ahmad May 30 '13 at 00:08
  • don't forget to declare the method "public static String[] serviceGetInfo();" that returns some info for creating Service, like to allow or not STOP, ... (read the doc for complete info) – bb67 May 30 '13 at 00:13
  • hm... ok. I have read many of your answers bb67. is JavaExe the easiest and efficient of all? any other recomendations? @djangofan also – Masood Ahmad May 30 '13 at 00:32
  • @bb67 getting info from the desktop would be similar? like capturing the desktop screen by Robot.class in Java? you have made it quite comprehensive though. Its different than showing input TO desktop rather its getting info FROM desktop. done differently? any code? last question :) – Masood Ahmad May 30 '13 at 00:36
  • JavaExe is the only one to my knowledge, can generate interactive services. But it only works on Windows. – bb67 May 30 '13 at 00:55
  • in the UI part, you can do every things that you do in a standard application. – bb67 May 30 '13 at 00:57
  • wow. thats the first time someone else just removed one of my comments and I have no idea who did it. – djangofan May 30 '13 at 04:27
  • @bb67 windows was the problem, in Linux i can just put it in some startup as root and then su and current user. Thanks. what about the Robot.class? – Masood Ahmad May 30 '13 at 12:57
  • @djangofan your comment was pretty good. I wonder who deleted it. you can post it again here though. – Masood Ahmad May 30 '13 at 12:58
  • java.awt.Robot class has a method named "createScreenCapture()" to capture a portion (or full) screen and return a BufferedImage object that you can save on disk as JPG with javax.imageio.ImageIO class. – bb67 May 30 '13 at 14:46
  • @bb67 now i have > 14 points. so +1+1 for you with thanks. My code in Robot is already ready. just wanted to be sure to cordinate with JavaExe as Robot captures the desktop screen. – Masood Ahmad May 31 '13 at 02:25
1

the main() method is never called in service mode (except one particular case), but if you want keep your main() method you must put a call to main() in serviceInit().

Put serviceInit() in your main class or in another class named XXX_ServiceManagement where XXX is the name of your main class.

Then, serviceInit() must return before a 30 seconds delay. Don't put a infinite loop, ... in it. Put your code in a thread, and start it from serviceInit() (or main)

That answer to your problem?

bb67
  • 160
  • 3
  • 10
  • can you elaborate further? do I need any external classes than myapp? the serviceinit() like edit-1 of my question. what is app? where its defined? – Masood Ahmad May 29 '13 at 18:51
  • I mean re edit your answer for me and others to learn and grasp the methodology. (helping community as a whole) – Masood Ahmad May 29 '13 at 18:59
  • all is clearly explain in the documentation of JavaExe and its examples – bb67 May 29 '13 at 19:22
  • it is recommanded to separate service classes and UI classes. Call your UI classes or methods only from XXX_TaskbarManagement. – bb67 May 29 '13 at 19:26
  • it's a sample variable use only for this example. – bb67 May 29 '13 at 19:41
  • you put what you want in serviceInit(), the only constraint is that this method should return before a period of 30 seconds (imposed by Windows) – bb67 May 29 '13 at 19:43
  • declared in com.devwizard.javaexe.examples.common.service.Examples_ServiceManagement, but this is an example !! NOT an obligation for JavaExe – bb67 May 29 '13 at 19:46
  • JavaExe itself calls this method and others are explained in the doc. it's not necesseray to use the Examples_ServiceManagement class or others classes in the examples. – bb67 May 29 '13 at 19:58
  • Thanks for the guidance! what about the service that is not being interactive to desktop? I wish to +1 when my score is > 15 – Masood Ahmad May 29 '13 at 23:07