tags:

views:

115

answers:

1

This might seem a bit awkward but I want to start a console program in Java but in it's own window, while at the same time using tee to save the output in a log file. I am porting the program that does this from Perl to Java and am having problems starting the program with lots of command argumments. In Perl it uses

system("dtterm -title $title -e /usr/bin/ksh -c \"".
"cd $dir; ./$exec $arg | tee $exec.log \" &");

I am trying to use the ProcessBuilder class to do the same.

ProcessBuilder builder = new ProcessBuilder("dtterm", "-title", title, "-e",
"/usr/bin/ksh", "-c" "./" + exec, arg, "|", "tee", exec + ".log");
builder.directory(dir);
Process process = builder.start();

While doing this, the process starts, it doesn't pipe into the log file as well, yet it does from the Perl. I have tried using the builder.getInputStream() method but since it is shown in the dtterm window there is no output from the out process.

I need to have the separate windows because the program launches several different programs at the same time, each with their own windows. I did try launching it directly and send the output from all the processes to my program stdout but there was too much, it needs to be kept separate. If there is no way of doing this then I will have to make a JFrame that shows the programs output but I would like to avoid that if possible.

Thanks

+1  A: 

You need to build up your command a bit differently. What you want is:

dtterm -title <title> -e /usr/bin/ksh -c "./<exec> arg | tee <exec>.log"

(Note the quotes around the parameter to ksh.) You need to replicate this in your code:

ProcessBuilder builder = new ProcessBuilder("dtterm", "-title", title, "-e", "/usr/bin/ksh", "-c", "./" + exec + " arg + " | tee " + exec + ".log");

Yes, this looks very ugly, and you have to watch out to escape exec properly in case it contains special characters like space, or quotes.

Bombe
Thanks, that didn't quite work but led me to it. Like in yours, everything after -c needs to be in 1 concatenated string but it doesn't need the quotes in the string. I don't know why this is but noticed it on another process where I was specifying a title, I had new ProcessBuilder("exec", "-title", "\"Program title\"") and the title showed up with the " on it. Took them out and it still worked. Maybe java keeps it all as 1 agument dispite spaces in the string.
Android
You’re right, of course. Java takes care of making sure that one parameter is handed to the program as a single parameter, the quotes are not necessary.
Bombe
Nevertheless you still need to escape special characters. :)
Bombe