PDA

View Full Version : [SOLVED] Escaping a Java String for the command line



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!

r-senior
April 24th, 2011, 03:07 PM
Doesn't the command and arguments need to be assembled into a String array as individual items?

So you'd have an array with "notify-send", "-i", "face-cool", etc.

http://download.oracle.com/javase/6/docs/api/java/lang/Runtime.html

tornadof3
April 24th, 2011, 03:46 PM
Great, that worked, thanks!