It is quite simple to run a Unix command from java.
Runtime.getRuntime().exec(myCommand);
But is it possible to run a Unix shell script from java code? If yes, would it be a good practice to run a shell script from within java code?
|
You should really look at Process Builder. It is really built for this kind of thing.
|
||||
|
I would say that it is not in the spirit of Java to run a shell script from Java. Java is meant to be cross platform, and running a shell script would limit its use to just UNIX. With that said, it's definitely possible to run a shell script from within Java. You'd use exactly the same syntax you listed (I haven't tried it myself, but try executing the shell script directly, and if that doesn't work, execute the shell itself, passing the script in as a command line parameter). |
|||||||||||||||||||||
|
I think you have answered your own question with
As to whether it is good practice... what are you trying to do with a shell script that you cannot do with Java? |
||||
|
You can use Apache Commons exec library also. Example :
For further reference, An example is given on Apache Doc also. |
|||||||||
|
Yes it is possible to do so. This worked out for me.
|
|||
|
I think with
Checking the operating system on can manage the shell/bash scrips if such are supported. if there is need to make the code portable. |
|||
|
As for me all things must be simple. For running script just need to execute
|
||||
|
It is possible, just exec it as any other program. Just make sure your script has the proper #! (she-bang) line as the first line of the script, and make sure there are execute permissions on the file. For example, if it is a bash script put #!/bin/bash at the top of the script, also chmod +x . Also as for if it's good practice, no it's not, especially for Java, but if it saves you a lot of time porting a large script over, and you're not getting paid extra to do it ;) save your time, exec the script, and put the porting to Java on your long-term todo list. |
|||
|
Just the same thing that Solaris 5.10 it works like this |
||||
|
But make sure that jdk version in which you compiled your application and the jdk version installed in your unix/linux should be same. |
|||
|