tornadof3
April 24th, 2011, 02:41 PM
I'm having trouble running a command line program from Java, mainly due to escape characters being sent literally. Any ideas?
import java.io.*;
public class notify {
public static void main(String[] args) {
try {
String $s = null;
String $cmdErr = null;
String $notifyTitle="\"Java Output\"";
String $notifyMsg="\"Notify Message\"";
String $command="notify-send -i face-cool " + $notifyTitle + " " + $notifyMsg;
System.out.println("Command is: " + $command);
//String $command="ls -l";
Process $p=Runtime.getRuntime().exec($command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader($p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader($p.getErrorStream()));
while(($s = stdInput.readLine()) != null) {
System.out.println("Command output: " + $s);
}
while(($cmdErr = stdError.readLine()) != null) {
System.out.println("Command errors: " + $cmdErr);
}
} catch(Exception $execErr) {
System.out.println($execErr.toString());
}
}
}
The Java executes fine but the notify-send command in ubuntu does not execute properly, generating an error of invalid number of arguments. But if you copy/paste the content of $command and run that with Alt-F2, the desired effect is fine!
import java.io.*;
public class notify {
public static void main(String[] args) {
try {
String $s = null;
String $cmdErr = null;
String $notifyTitle="\"Java Output\"";
String $notifyMsg="\"Notify Message\"";
String $command="notify-send -i face-cool " + $notifyTitle + " " + $notifyMsg;
System.out.println("Command is: " + $command);
//String $command="ls -l";
Process $p=Runtime.getRuntime().exec($command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader($p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader($p.getErrorStream()));
while(($s = stdInput.readLine()) != null) {
System.out.println("Command output: " + $s);
}
while(($cmdErr = stdError.readLine()) != null) {
System.out.println("Command errors: " + $cmdErr);
}
} catch(Exception $execErr) {
System.out.println($execErr.toString());
}
}
}
The Java executes fine but the notify-send command in ubuntu does not execute properly, generating an error of invalid number of arguments. But if you copy/paste the content of $command and run that with Alt-F2, the desired effect is fine!