If you happen to have command execution via Java's Runtime.exec
on a Unix system, you may already have noticed that it doesn't behave like a normal shell. Although simple commands like ls -al
, uname -a
, or netstat -ant
work fine, more complex commands and especially commands with indispensable features like pipes, redirections, quoting, or expansions do not work at all.
Well, the reason for that is that the command passed to Runtime.exec
is not executed by a shell. Instead, if you dig down though the Java source code, you'll end up in the UNIXProcess class, which reveals that calling Runtime.exec
results in a fork
and exec
call on Unix platforms.
Nonetheless, I'll show you a way to still get commands executed in a proper shell.