I am not able to understand why and in what kind of scenario I would like to run scripting language inside JVM at server side. I mean we already have great libraries available in JAVA... why wouldn't I use use them to do stuff instead of running scripts in JVM? Please help me understand this concept, Can someone please tell me one or two use cases where running scripting in JVM at server side will have advantages over using JAVA libraries.
-
This is an opinion based question. But anyway, having more choice doesn't take away something. Furthermore, Java has great libraries, but do they cover everything? no. Always choose the tool that is best for your current task. Sometimes that is Java, sometimes that is Javascript, and sometimes even PERL. – Dylan Meeus Aug 04 '16 at 10:52
2 Answers
The more general question here is "Why would I want to execute code in <some scripting language> in Java?", where Javascript is simply one example of such a language.
Quoting this article:
Some Java applications' requirements make integration with a scripting language necessary. For example, your users may need to write scripts that drive the application, extend it, or contain loops and other flow-control constructs. In such cases, it's sensible to support a scripting language interpreter that can read user scripts, then run them against your Java application's classes.
Basically: if you want to allow your users to customize your application in a way that requires the richness of a programming language.
Why Javascript specifically? That depends upon your application. There may be reasons to pick that (e.g. your target users already know Javascript), or not (e.g. Lua is more popular for scripting in the games industry).

- 137,514
- 11
- 162
- 243
One use i can think of is:
- Javascript engine can be used to evaluate arithmetic expressions which are in a String format.
example :: String expression = "2+4";
The below is the link to a example which uses java script engine to do this.
-
"Java doesn't provide any way to achieve this" Of course it does. You can implement an arithmetic expression parser and evaluator in Java. – Andy Turner Aug 04 '16 at 10:55
-
@AndyTurner i meant java doesn't have a built in method to achieve this, at least as far as i know. Please reply if there is one. Anyway I have removed that statement from my answer. – Abhishek Aug 04 '16 at 10:59