Reputation: 307
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
Reputation: 3724
You have plenty of options:
You can use RunTime class. Runtime getRuntime() exec(String command)
Look Runtime for more information.
You can use ProcessBuilder. Process p = new ProcessBuilder("myCommand", "myArg").start();
Look into ProcessBuilder for more information.
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