Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaExe and Java application as windows system service interactive to desktop

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?

like image 209
Masood Ahmad Avatar asked Oct 22 '22 08:10

Masood Ahmad


1 Answers

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?

like image 64
bb67 Avatar answered Oct 27 '22 16:10

bb67