PDA

View Full Version : [SOLVED] Running a command in java



stinkinrich88
May 6th, 2007, 11:21 AM
Hello!

can anyone explain to me why this:

try{Process p = Runtime.getRuntime().exec("halt -p");}
catch(Exception nFE){}

use to shut my computer down in fedora, but now I've changed to ubuntu, it doesnt!

thanks!

Ramses de Norre
May 6th, 2007, 11:26 AM
Is it ran with root privileges?

stinkinrich88
May 6th, 2007, 01:11 PM
Is it ran with root privileges?

Don't think so... But then again it wasn't in Fedora either...

Would running the application as root be enough or would i need extra coding?

stinkinrich88
May 6th, 2007, 01:15 PM
actually, I'm pretty sure this isn't the problem as when I replace the command with this:

try{Process p = Runtime.getRuntime().exec(/*"halt -p"*/"echo hello");}
catch(Exception nFE){}

'hello' does not get printed to standard output

phossal
May 6th, 2007, 01:18 PM
What do you use such a program for? Why use Java to shut down your pc?

stinkinrich88
May 6th, 2007, 01:23 PM
What do you use such a program for? Why use Java to shut down your pc?

Well, basically, I made a shutdown timer so I can watch a DVD in bed and have the computer shutdown itself when the specified time runs out! Lazy, yes, but so handy

phossal
May 6th, 2007, 01:40 PM
Here:


package com;

import java.io.*;

public class ShutDown {


public ShutDown() {
try {
// Execute command
String command = "sudo poweroff";
Process child = Runtime.getRuntime().exec(command);

// Get output stream to write from it
OutputStream out = child.getOutputStream();

out.write("PASSWORD".getBytes()); //ADD YOUR PASSWORD
out.close();
} catch (IOException e) {
}
}

public static void main(String args[]) {
ShutDown s = new ShutDown();
}
}


------------------------------------------
If you man halt, which is a funny audible command, you'll realize that its invocations are spicier than they once were. Instead of the simple halt, you have reboot, poweroff, etc. Also, the average dope who uses the getRuntime.exec() method hardly ever writes bytes back to it. How many good command line arguments can you send that don't require some additional input? (In Windows, I often call batch (.bat) files to do my bitch work rather than using the exec() for all of it.) Anyway, that's ^ power. Use it wisely.

phossal
May 6th, 2007, 01:54 PM
The fun part is, I know you'll be gone awhile before you get to post back. Because it works, you'll spend at least half the time between posts restarting your machine. :)

Ayman
May 6th, 2007, 02:54 PM
While unrelated, this may also solve your problem: use the "at" command. For example:

$ at now + 2 hours
at> shutdown -h now
at> <EOT>

man at for more info.

Another option is command chaining, which I use in situations similar to yours:

$ command1 && command2
In the above snippet, command2 will be executed only after command1 successfully finishes. If command1 fails, command2 will not be run.

stinkinrich88
May 6th, 2007, 03:09 PM
Here:

------------------------------------------
If you man halt, which is a funny audible command, you'll realize that its invocations are spicier than they once were. Instead of the simple halt, you have reboot, poweroff, etc. Also, the average dope who uses the getRuntime.exec() method hardly ever writes bytes back to it. How many good command line arguments can you send that don't require some additional input? (In Windows, I often call batch (.bat) files to do my bitch work rather than using the exec() for all of it.) Anyway, that's ^ power. Use it wisely.

Ahh!! Worked a treat! Thanks!

So I did need root privileges after all! (my echo example was bad because it didn't work anyway!)

Shame it needs my password... but I suppose at least it doesnt have to be hard-coded (I could ask for it on load... but that would involve typing (even that's annoying just before bed!))

I don't mind restarting ubuntu! It's way quicker than fedora! Plus firefox remembered my page, and MPD carried on where it left off! Excellent!

Thanks!!!!

And thanks Ayman! I'll check it out!

phossal
May 6th, 2007, 03:14 PM
Stellar. :) Happy to help, of course. Thanks for posting back.

Ramses de Norre
May 6th, 2007, 08:32 PM
sudo shutdown -h <time in minutes>