zjffdu
zjffdu

Reputation: 28794

How to communicate with a shell session via Java

I'd like to run some shell command in a java program. But I don't want to launch the shell script as a Process and capture its output. I just wonder is there any existing library to launch a shell session in java and communicate with it ? Some kind of library that I can use API directly. such as following. Thanks

Shell shell = new Shell();
String output = shell.execute("pwd");
String errorOutput = shell.execute("wrong command");

Upvotes: 1

Views: 842

Answers (2)

Cet
Cet

Reputation: 11

The class ProccessBuilder might help.

I also followed Mkyong tutorial once and it was really useful.

Upvotes: -1

Víctor López
Víctor López

Reputation: 817

In Java, you can use Runtime.getRuntime().exec to execute shell commands.

It's a pretty easy to use it.

Here you have a very good tutorial: https://www.mkyong.com/java/how-to-execute-shell-command-from-java/

Upvotes: 2

Related Questions