我的代码如下,我创建了我的 jar 文件。当我从以管理员身份运行的 cmd 执行我的 jar 文件时,jar -jar myfile.jar
它可以工作。
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();
}
}
}
我得到这样的输出......
Output of running [cmd.exe, /c, sc, start, TestWindowsService] is:[SC] StartService: OpenService FAILED 5:
Access is denied.
我从这个问题中了解到的是......我cmd.exe
正在从不在管理中的java代码打开我的......
我怎么能解决..这个....?
提前致谢....