You can execute shell commands and script files directly from Java, using java.lang.Runtime. It's a bit more complex than it seems, search this forum or internet for in depth explanations.
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Joined: Jun 26, 2007
Thank you Martin. That is one way but that will be platform specific.
I am trying to do something by using the java 1.6 ScriptManager where we can get different script engine and execute respective script.
To explain my point I am giving the following example.
The above test code uses the following codes.js file
Once I run the code It will print 21.
Now my question is that instead of this .js file if I have a unix shell script which contains some function then is there a way to execute in the same way? For that we require a separate ScriptEngine. So is there already some script engine present?
No shell script engine exists to my knowledge (see https://scripting.dev.java.net/ for an incomplete list of available engines). Such an engine would also face some practical difficulties as there are numerous different shells, each with its own syntax.
The best approach is probably what Martin suggested: use Runtime.exec or java.lang.ProcessBuilder.
That would be interesting, though... aside from some really OS-specific stuff (like nohup and devices etc.) I don't see why it couldn't be done, assuming Java implementations of the likely suspects. Hmm.
The thing is, shell scripts are full of built-in commands and calls to other tools. In the end, unless you limit yourself to only built-in commands, you're going to need ProcessBuilder at some time. Instead of managing the input / output of all those processes you might as well use only one external process, to the shell script.
No, I'm saying if the usual suspects had internal Java implementations. Some already do, like grep. It'd be fairly straight-forward to provide the other common ones, like sed, awk, cut, etc. The shell language itself would be easy(ish). Not saying it's a good idea, just saying it's doable, and interesting.