java.lang.runtime.exec()

M

Mitschu

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
 
R

RedGrittyBrick

Mitschu said:
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 );
}

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.
 
G

Gordon Beaton

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" );

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


--
 
N

Nigel Wade

Mitschu said:
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();

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?&..._miny=2007&as_maxd=13&as_maxm=12&as_maxy=2007>
 
H

Hendrik Maryns

Nigel Wade schreef:
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?&..._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.
--
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


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.4-svn0 (GNU/Linux)
Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org

iD8DBQFHYWqae+7xMGD3itQRAtGMAJ9vVCXk2aQaIXkBTTNQXDkO9bu8VACfWVH0
kx9+0MIVJJDnLW+dwC4FAe0=
=f8MM
-----END PGP SIGNATURE-----
 
R

Roedy Green

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top