Search code examples
javaspringdebian-jessie

Using Java, In debian 8 how to we restart networking and another services?


My server is debian 8 (jessi) , when i restart networking using following java code then i get warning "Faild to restart Networking.service: Access denied."

 try
    {            
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec("systemctl restart networking");
        InputStream stderr = proc.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<ERROR>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</ERROR>");
        int exitVal = proc.waitFor();
        System.out.println("Process exitValue: " + exitVal);
      } catch (Throwable t)
      {
        t.printStackTrace();
      }

Can any one solve it ? Thanks in Advance.


Solution

  • Ohh, finally i get the solution, here i changed in my nano /etc/sudoers and change after the line,

        ` %sudo ALL=(ALL:ALL) ALL`
    
    `sushil ALL=(ALL) NOPASSWD: ALL`
    

    here "sushil" is my system user name.