I have a requirement to invoke unix shell script, however, I understand that having the runtime.exec() in EJB is not recommended, how about in serlvet? Or any other better alternatively?
If it is a requirement, you have to do it although it is not recommended in the specs. That's life sometimes!
My recommendation on implementation is:
- Define an interface with a method doWhatIWantToDo().
- Implement MyOperatingSystemImplementation, a class implementing the interface. Let this implemenation call exec(...).
- Define "implementing class" as a property. (In the DD or somewhere else depending on how you define properties in the project.)
- Make a small factory using the "implementing class" information to return an object working on the operating system you have.
With this solution, you can add new implementations (for other operating systems) without changing the existing code. But most of all, it is clear to persons working with the code in the future that this implementations is a little dirty!
And if someone blames you for a "dirty" solution using OS calls, you can always say that you know and have made a good encapsulation of it.