Recently I am stuck with a Java program. My requirement is, I have to restart my snmp service through java code in my Ubuntu machine. Normally We can do the same with
Runtime.getRuntime().exec("service snmpd restart");
Above code is working fine if I log in to the system with ROOT user. But now the requirement came that, it may possible client machine doesn't have root permission. In that case to restart the snmp one need to execute the command with sudo "sudo service snmpd restart". This command will ask for machine password and after entering the password system will restart the service.
Now whenever I am trying to execute the java code with the below code, it's not restarting the service. ecasue it doesn't have the option to receive the password. Runtime.getRuntime().exec("sudo service snmpd restart");
So, please help me to find out a way to restart a service with java when user is not a root user and need to start a service with sudo command.