-1

I want to register the service in windows and after register wants to start also.

using following command in the code::

cmd.exe /C sc create "<service name>" binpath="D:\\abc\\xyz.exe -zglaxservice xyz"

I execute above command through runtime.exec().

upon running the code it gives the help of sc command.

Code ::

Process proc = null;
String[] cmdStr = new String[] { "cmd.exe","/C","sc", "create", "\""+servicename+"\"", "binpath= \"D:\\SCCode\\"+exeName+".exe -zglaxservice "+laxName+"\"" };
proc = Runtime.getRuntime().exec(cmdArr);

Please help...

Anjali
  • 1,623
  • 5
  • 30
  • 50

2 Answers2

0

Two issues with the command

cmd.exe /C sc create "" binpath="D:\abc\xyz.exe -zglaxservice xyz"
  1. Service name is missing
  2. After = (equals symbol) you need to put a space.

So the correct command would be as follows:

cmd.exe /C sc create "ServiceName" binpath= "D:\abc\xyz.exe -zglaxservice xyz"

I observed you did some modification, although still you command line string is not correct (couple of space is missing). The correct one would be as follows:

String[] cmdStr = new String[] { "cmd.exe ","/C ","sc ", "create ", "\""+servicename+"\"", " binpath= \"D:\\SCCode\\"+exeName+".exe -zglaxservice "+laxName+"\"" };

I tried above and I was getting issue as

'"sc "' is not recognized as an internal or external command

which comes if your java is not running in admin mode, since sc command requires your cmd command to run in admin mode.

If you are facing the same issue, please check below link:

Run command prompt as Administrator

Community
  • 1
  • 1
sakura
  • 2,249
  • 2
  • 26
  • 39
  • tried you inputs but doesnt work. service name i have already added. you can also check the code i have added in the post. – Anjali Oct 13 '14 at 07:34
  • @Anjali added more clues. – sakura Oct 13 '14 at 07:52
  • password is needed if i run it as an admin,I want to do this without providing password....is there any way? – Anjali Oct 13 '14 at 10:46
  • @Anjali check this --> http://superuser.com/questions/244959/run-as-administrator-shortcut-without-password-prompt – sakura Oct 13 '14 at 11:03
0

This solution works for me:

cmd.exe /C sc create "cdfa$" binpath= "F:\Connect_Direct\Sterling Commerce\FileAgent\cdfa$.exe -zglaxservice cdfa$"

harmonica141
  • 1,389
  • 2
  • 23
  • 27
yogesh
  • 1