@@ -206,22 +206,28 @@ public void spawn(List<String> args) throws Exception {
206
206
207
207
@ Override
208
208
public Installer addServiceRegistration (String user ) {
209
+ log .warn ("Registering system service: {}" , PROPS_FILE );
209
210
if (!SystemUtilities .isAdmin ()) {
210
211
throw new UnsupportedOperationException ("Installing a service requires elevation" );
211
212
}
212
213
214
+ if (WindowsUtilities .serviceExists (PROPS_FILE )) {
215
+ log .warn ("System service is already registered, removing." );
216
+ removeServiceRegistration ();
217
+ }
218
+
213
219
Path nssm = SystemUtilities .getJarParentPath ().resolve ("utils/nssm.exe" );
214
220
Path qz = SystemUtilities .getJarParentPath ().resolve (PROPS_FILE + ".exe" );
215
- String servicePath = String .format ("\" " + qz .toString () + "\" %s %s %s" ,
216
- ArgValue .WAIT .getMatches ()[0 ],
217
- ArgValue .STEAL .getMatches ()[0 ],
218
- ArgValue .HEADLESS .getMatches ()[0 ]);
219
221
220
222
// Install the service
221
- if (ShellUtilities .execute (nssm .toString (), "install" , PROPS_FILE , servicePath )) {
222
- ShellUtilities .execute (nssm .toString (), "set" , "DisplayName" , ABOUT_TITLE );
223
- ShellUtilities .execute (nssm .toString (), "set" , "Description" , ABOUT_DESCRIPTION );
224
- ShellUtilities .execute (nssm .toString (), "set" , "DependOnService" , "Spooler" );
223
+ if (ShellUtilities .execute (nssm .toString (), "install" , PROPS_FILE ,
224
+ qz .toString (),
225
+ ArgValue .WAIT .getMatches ()[0 ],
226
+ ArgValue .STEAL .getMatches ()[0 ],
227
+ ArgValue .HEADLESS .getMatches ()[0 ])) {
228
+ ShellUtilities .execute (nssm .toString (), "set" , PROPS_FILE , "DisplayName" , ABOUT_TITLE );
229
+ ShellUtilities .execute (nssm .toString (), "set" , PROPS_FILE , "Description" , ABOUT_DESCRIPTION );
230
+ ShellUtilities .execute (nssm .toString (), "set" , PROPS_FILE , "DependOnService" , "Spooler" );
225
231
log .info ("Successfully registered system service: {}" , PROPS_FILE );
226
232
if (user != null && !user .trim ().isEmpty ()) {
227
233
log .info ("Setting service to run as {}" , user );
@@ -233,7 +239,9 @@ public Installer addServiceRegistration(String user) {
233
239
TaskKiller .killAll ();
234
240
// Instruct autostart to be ignored
235
241
FileUtilities .disableGlobalAutoStart ();
242
+ log .info ("Starting system service: {}" , PROPS_FILE );
236
243
if (WindowsUtilities .startService (PROPS_FILE )) {
244
+ log .info ("System system service started successfully." , PROPS_FILE );
237
245
return this ;
238
246
}
239
247
}
@@ -242,20 +250,25 @@ public Installer addServiceRegistration(String user) {
242
250
243
251
@ Override
244
252
public Installer removeServiceRegistration () {
253
+ log .info ("Removing system service: {}" , PROPS_FILE );
245
254
if (!SystemUtilities .isAdmin ()) {
246
255
throw new UnsupportedOperationException ("Removing a service requires elevation" );
247
256
}
248
257
249
- WindowsUtilities .stopService (PROPS_FILE );
250
- Path nssm = SystemUtilities .getJarParentPath ().resolve ("utils/nssm.exe" );
251
- if (ShellUtilities .execute (nssm .toString (), "remove" , PROPS_FILE , "confirm" )) {
252
- // Old tutorials used "QZ Tray" as the service name
253
- ShellUtilities .execute (nssm .toString (), "remove" , ABOUT_TITLE , "confirm" );
254
- // Restore default autostart settings by deleting the preference file
255
- FileUtils .deleteQuietly (FileUtilities .SHARED_DIR .resolve (AUTOSTART_FILE ).toFile ());
256
- log .info ("System service successfully removed: {}" , PROPS_FILE );
258
+ if (WindowsUtilities .serviceExists (PROPS_FILE )) {
259
+ WindowsUtilities .stopService (PROPS_FILE );
260
+ Path nssm = SystemUtilities .getJarParentPath ().resolve ("utils/nssm.exe" );
261
+ if (ShellUtilities .execute (nssm .toString (), "remove" , PROPS_FILE , "confirm" )) {
262
+ // Old tutorials used "QZ Tray" as the service name
263
+ ShellUtilities .execute (nssm .toString (), "remove" , ABOUT_TITLE , "confirm" );
264
+ // Restore default autostart settings by deleting the preference file
265
+ FileUtils .deleteQuietly (FileUtilities .SHARED_DIR .resolve (AUTOSTART_FILE ).toFile ());
266
+ log .info ("System service successfully removed: {}" , PROPS_FILE );
267
+ } else {
268
+ log .error ("An error occurred removing system service: {}" , PROPS_FILE );
269
+ }
257
270
} else {
258
- log .error ( "An error occurred removing system service: {}, please try to remove manually using 'sc " , PROPS_FILE );
271
+ log .info ( "System service was not found, skipping." );
259
272
}
260
273
261
274
return this ;
0 commit comments