Shashank
Shashank

Reputation: 307

How can I execute shell script from a jar file

I am using shell commands inside java. When I run the project in netbeans it is working well. But when I run the jar file of the project through the command line it is giving this error

java.io.IOException: Cannot run program "sudo rabbitmqctl list_queues": error=2, No such file or directory

Can you tell if there is a way to run shell commands from jar file?

Upvotes: 1

Views: 1600

Answers (1)

Somnath Musib
Somnath Musib

Reputation: 3724

You have plenty of options:

  1. You can use RunTime class. Runtime getRuntime() exec(String command) Look Runtime for more information.

  2. You can use ProcessBuilder. Process p = new ProcessBuilder("myCommand", "myArg").start(); Look into ProcessBuilder for more information.

  3. You can use jsch API to execute Linux/Unix commands from your local machine to any remote system. You can create a shell script in your java code ( if you are running multiple shell commands) and then set up a jsch connection to the remote server. With the jsch connection, you can copy the shell script to remote server and run the entire shell script. I find this 3rd option very useful.

Upvotes: 1

Related Questions