sawan
sawan

Reputation: 2381

Access denied to cmd while executing my java code

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

Answers (1)

Sandhu Santhakumar
Sandhu Santhakumar

Reputation: 1688

  1. Click Start and type CMD in the Start search box
  2. Right click on CMD in the list and click on "Run as administrator"

Upvotes: 2

Related Questions