2013-11-15 34 views
0

我的代码如下,我创建了我的jar文件。当我执行从命令jar -jar myfile.jar作为管理员运行的cmd的我的jar文件它的作品。执行我的java代码时拒绝访问cmd

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代码是不是在行政..

我该怎么解决......这个....?

在先进的感谢....

+0

你用什么命令编译/运行? –

+0

java -jar myjarname.jar – swan

回答

2
  1. 单击开始,然后键入cmd在开始搜索框中
  2. 右键单击列表中的CMD,然后单击“以管理员身份运行”