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.