2

Testing out nssm with a program which is as simple as:

import javax.swing.JOptionPane;

// a simple program to test if i can get nssm to work
public class Test {

    public static void main(String[]args) throws InterruptedException {
        while(true) {
            JOptionPane.showMessageDialog(null, "Test");
            Thread.sleep(5000);
        }
    }
}

I created a jar file out of the above program, added nssm to the PATH and used the following to create a service out of it:

C:\Users\ProgrammerSausage>nssm install servicetest "C:\ProgramFiles\Java\jre1.8.0_162\bin\java.exe" "-jar C:\Users\ProgrammerSausage\Documents\serviceTest.jar"

Administrator access is needed to install a service.

I enter my password - it seems to have worked. Checked task manager services, it's there, but "stopped", i enable to see what might happen, says it's running. But nothing. What am i doing wrong?

EDIT:

I've changed the console to admin. Created a .bat (which works on its own):

@echo off
java -jar C:\Users\ProgrammerSausage\Documents\serviceTest.jar

However, if i try to install the batch file with NSSM, it still just opens a phantom service which does nothing besides say it is running.

C:\Windows\system32>nssm install servicetestfour "C:\Users\ProgrammerSausage\Documents\serviceTest.bat"

Service "servicetestfour" installed successfully!

What I find interesting, is that I can create a phantom service of ANY directory, such as:

C:\Windows\system32>nssm install servicetestfive "C:\Users\ProgrammerSausage\"

Service "servicetestfive" installed successfully!

Erm what? The major difference however is that services which are created from random directories don't start. This suggests therefore that it can run the batch and jar files, but I don't see the JOptionPane popup?

EDIT:

Ok, so I've changed the code of the program because as suggested, it was likely not allowing the gui to show, to include a simple io call:

public static void main(String[]args) throws InterruptedException {
    File f = new File("C:\\Users\\ProgrammerSausage\\Documents\\test");
    while(true) {
        if(!f.exists()) {
            f.mkdir();
        }
        JOptionPane.showMessageDialog(null, "Test");
        Thread.sleep(5000);
    }
}

This is very interesting, because although i have enabled

'Allow service to interact with desktop'

The JOptionPane does not show, but the mkfile() method is still called, though only once: Which means the program is running, but the JOptionPane is hidden in the background grabbing modal control and stopping the loop, and if the gui is removed, then io works flawlessly.

John
  • 346
  • 1
  • 11

1 Answers1

0

Assuming the jar you've created is a RUNNABLE jar (that can be run from a terminal using java -jar ...)...

I'd suggest creating a script that runs the jar...

run_serviceTest.bat:

@echo off
java -jar C:\Users\ProgrammerSausage\Documents\serviceTest.jar

Then you could create the nssm service based on that script:

nssm install servicetest run_serviceTest.bat

Try running the command on a terminal with admin privileges.

Nssm will ask for the "current dir" to start the service. Select the directory that contains the jar.

I see your service uses JOptionPane which is a GUI component. If you want your services to interact with desktop , you need to enable it in the service administrator:

enter image description here

You could also enable desktop interaction from nssm:

enter image description here

Services usually do not interact with the desktop.

Martín Zaragoza
  • 1,717
  • 8
  • 19
  • yep, the jar is runnable on its own, through terminal and through batch. The service which nssm creates just doesn't seem to work correctly, even through a batch file. – John Aug 10 '18 at 04:02
  • What error are you getting when trying to start the service? – Martín Zaragoza Aug 10 '18 at 12:07