Running Unix Programs From Java

This is my third attempt at posting this subject, I hope it works.How do I run UNIX commands/programs/shell scripts from within Java and capture the return status and output?Thank you.
[205 byte] By [BuzzHusea] at [2007-11-27 9:04:42]
# 1
Using Runtime.exec and the Process class.But it is very important that you read this article http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.htmlIt explains many of the common mistakes, "gotchas" and tricks to using Runtime.exec and the Process class properly.
cotton.ma at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 2
Am I doing something wrong, or am I just a lucky stiff - I've never had to rely on Runtime.exec?
BigDaddyLoveHandlesa at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 3

> Am I doing something wrong, or am I just a lucky

> stiff - I've never had to rely on Runtime.exec?

You're lucky.

I had to use it once, to invoke ghostview or ghostscript to print a PDF doc. Ffuuggly business, that.

Runtime.exec asks Chuck Norris what it should do.

jverda at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 4
Thank you very much. I got the example to work on my WinXP PC & uploaded it to a UnixWare 7.1.1 system. It works there except I have to figure out to pass it a "*" to a "ls -l" command.
BuzzHusea at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 5

*, >, | &, etc. are interpreted by the shell.The command you're executing never sees them. When you rt.exec some command, you're bypassing the interpretation of those characters.

When you're in a terminal window, and you type ls -l *

that string of characters goes to the shell first, which turns * into aaa bbb ccc or whatever is in that directory, and then the shell invokes ls -l aaa bbb ccc.

The details differ by shell and *nix install, but roughly...

rt.exec("/bin/bash -c 'ls -l *'");

or

rt.exec(new String[] {"/bin/bash", "-c", "ls -l *"});

See the man page for your shell, but here, -c tells bash "take everything that follows as a command and its args to execute."

Also, you'll want to read this:

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

jverda at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 6
I realize you're probably just testing with "ls -l *", but in case you aren't: a much better way to get a file listing in Java is to use the java.io.File.list* methods.
paulcwa at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 7
Thank you, that worked. You guys are a gold mine. I took Basic Java and Advanced Java back in 2003, fell in love with it. Due to work and other things, I went off to do other stuff and now have an opportunity to actually use it.
BuzzHusea at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 8
Thank you, I have discovered that the hard way. "Find" doesn't work too well, either. That's OK, there is really only one program I need to call, a compiled C program. Tomorrow, I will get around to seeing if I can call that one successfully.
BuzzHusea at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...
# 9

> Thank you, I have discovered that the hard way.

> "Find" doesn't work too well, either.

You're quite welcome.

There's no reason find shouldn't work though. This worked for me. I'm running XP and have cygwin installed

rt.exec("C:/cygwin/bin/bash -c 'find /tmp -print > /tmp/qweryui'");

jverda at 2007-7-12 21:38:04 > top of Java-index,Java Essentials,Java Programming...