Reputation: 2381
I have my code as following and i have created my jar file. when i execute my jar file from cmd which is runing as administrator with command jar -jar myfile.jar
it works.
public class ServiceStartStop {
private static String SERVICE_NAME = "TestWindowsService";
public static void main(final String[] args) {
// to start service
final String[] StartScript = {"cmd.exe", "/c", "sc", "start", SERVICE_NAME};
try {
final Runtime runtime = Runtime.getRuntime();
final Process process = runtime.exec(StartScript);
final InputStream is = process.getInputStream();
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(StartScript));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
}
I am getting output like this...
Output of running [cmd.exe, /c, sc, start, TestWindowsService] is:[SC] StartService: OpenService FAILED 5:
Access is denied.
What i have understood from this problem is that...i am opening my cmd.exe
from java code which is not in administrative...
How could i solve..this....?
THANKS IN ADVANCED....
Upvotes: 0
Views: 2289
Reputation: 1688
Upvotes: 2