Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java.lang.runtime.exec()

Reply
Thread Tools

java.lang.runtime.exec()

 
 
Mitschu
Guest
Posts: n/a
 
      12-13-2007
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
 
Reply With Quote
 
 
 
 
RedGrittyBrick
Guest
Posts: n/a
 
      12-13-2007
Mitschu wrote:
> 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.
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      12-13-2007
On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu wrote:
> 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


--
 
Reply With Quote
 
Nigel Wade
Guest
Posts: n/a
 
      12-13-2007
Mitschu wrote:

> 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?&as_epq=runtime.exec&as_ugroup=comp.lang.ja va.*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&a s_maxd=13&as_maxm=12&as_maxy=2007>

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail :
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
Reply With Quote
 
Hendrik Maryns
Guest
Posts: n/a
 
      12-13-2007
Nigel Wade schreef:
> Mitschu wrote:
>
>> 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?&as_epq=runtime.exec&as_ugroup=comp.lang.ja va.*&as_drrb=b&as_mind=1&as_minm=12&as_miny=2007&a s_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+7xMGD3itQRAtGMAJ9vVCXk2aQaIXkBTTNQXD kO9bu8VACfWVH0
kx9+0MIVJJDnLW+dwC4FAe0=
=f8MM
-----END PGP SIGNATURE-----

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-13-2007
On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
<> 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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roger Lindsjö
Guest
Posts: n/a
 
      12-13-2007
Roedy Green wrote:
> On Thu, 13 Dec 2007 07:54:21 -0800 (PST), Mitschu
> <> 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.


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ö
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57