Hello
I'm using java.lang.runtime.exec() in Windows enviroment without
problems. Now on a linux Server it won't run. Can you see an error?
Has the string to be terminated with sth? I tried \r and \n without
success.
try {
Runtime runtime = Runtime.getRuntime();
Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );
p.waitFor();
out.println("Exit status: " + p.exitValue() );
}catch(Exception e) {
out.println("Error: " + e );
}
Thanks for any help.
Michael
RedGrittyBrick - 13 Dec 2007 16:46 GMT
> Hello
> I'm using java.lang.runtime.exec() in Windows enviroment without
[quoted text clipped - 10 lines]
> out.println("Error: " + e );
> }
I've never used Runtime.exec() but from prior discussions here I vaguely
recall that you need to feed it the name of a native executable. In your
case, the name of a shell should be the first thing you give to exec().
The API docs and/or Google Groups may well be your friends.
If you really are using echo to append to a file, I suggest you use Java
IO classes instead.
Gordon Beaton - 13 Dec 2007 16:47 GMT
> I'm using java.lang.runtime.exec() in Windows enviroment without
> problems. Now on a linux Server it won't run. Can you see an error?
[quoted text clipped - 4 lines]
> Runtime runtime = Runtime.getRuntime();
> Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );
The main problem is that you are using shell features (redirection)
but Runtime.exec() does not invoke a shell.
To RGB: Although echo is built into many shells it's also a real
executable on Unix, so that isn't the reason a shell is necessary
here.
There may be additional problems with your *real* command if you are
using quotation marks to group arguments, since the command line is
tokenized without regard to quoting or escaping when you use
exec(String). A better choice is usually exec(String[]).
Try it like this:
String[] cmd = { "/bin/sh", "-c", "echo 'tttt' >> /var/test/txt" };
/gordon
--
Nigel Wade - 13 Dec 2007 17:02 GMT
> Hello
> I'm using java.lang.runtime.exec() in Windows enviroment without
[quoted text clipped - 4 lines]
> try {
> Runtime runtime = Runtime.getRuntime();
This subject was done to death last week (or was it the week before). Please
look through the Google groups archive for the comp.lang.java.* groups:
<http://groups.google.co.uk/groups?&as_epq=runtime.exec&as_ugroup=comp.lang.java.
*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&as_maxd=13&as_maxm=12&as_maxy=2007>

Signature
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Hendrik Maryns - 13 Dec 2007 17:23 GMT
Nigel Wade schreef:
>> Hello
>> I'm using java.lang.runtime.exec() in Windows enviroment without
[quoted text clipped - 9 lines]
>
> <http://groups.google.co.uk/groups?&as_epq=runtime.exec&as_ugroup=comp.lang.java.
*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&as_maxd=13&as_maxm=12&as_maxy=2007>
Indeed, and the thread was started by me.
In short:
- use ProcessBuilder instead of Runtime.exec (Java 5+)
- redirection will not work except if you invoke a shell and tell it to
run the command
- better read the output from the process and write it to a file
yourself, in a separate thread (watch out for the names of the methods)
Read http://mindprod.com/jgloss/exec.html.
Much luck, H.

Signature
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green - 13 Dec 2007 21:31 GMT
On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
<michael.arnold@sisag.ch> wrote, quoted or indirectly quoted someone
who said :
> Process p = runtime.exec( "echo 'tttt' >> /var/test.txt" );
See http://mindprod.com/jgloss/exec.html
You are making the most common error. echo is not an executable. It
is a command to the command interpreter, as is >> (at least it is in
Windows, don't know about your platform).
You must spawn a copy of a command interpreter, and feed it that line
as a parameter.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roger Lindsjö - 13 Dec 2007 22:27 GMT
> On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
> <michael.arnold@sisag.ch> wrote, quoted or indirectly quoted someone
[quoted text clipped - 5 lines]
>
> You are making the most common error. echo is not an executable.
Probably one of the most common errors, but on my machine there is a
/bin/echo. There is also a build in echo in my shell (bash). Does that
mean I can both eat the cake and have it too? ;-)
//Roger Lindsjö