I am trying to do a simple shutdown program in JAVA and I can't believe that I couldn't find an answer to this anywhere else.
I first tried using sudo in my java program:
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class themain{
public static void main(String[] args){
Process ls=null;
BufferedReader input=null;
String line=null;
String[] cmd = {"sudo shutdown -h +20"};
try {
ls= Runtime.getRuntime().exec(cmd);
input = new BufferedReader(new InputStreamReader(ls.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
try {
while( (line=input.readLine())!=null)
System.out.println(line);
} catch (IOException e1) {
e1.printStackTrace();
System.exit(0);
}
}
}
Then I tried executing a shell script with this code in it:
sudo shutdown -h +20
The new java program now looked like this:
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class themain{
public static void main(String[] args){
Process ls=null;
BufferedReader input=null;
String line=null;
String[] cmd = {"sh shutdown.sh"};
try {
ls= Runtime.getRuntime().exec(cmd);
input = new BufferedReader(new InputStreamReader(ls.getInputStream()));
} catch (IOException e1) {
e1.printStackTrace();
System.exit(1);
}
try {
while( (line=input.readLine())!=null)
System.out.println(line);
} catch (IOException e1) {
e1.printStackTrace();
System.exit(0);
}
}
}
This of course didn't work either... Is there anyway I could invoke a password graphical password prompt? And I want this program to work on every computer, so I don't want to mess up my individual sudoers file...
Regards, and thanks