178 CHAPTER 9 SCRIPTING AND JSR 223
CHAPTER 9 SCRIPTING AND JSR 223 179 try { engine.eval( “function reverse(name) {” + ” var output = ‘’;” + ” for (i = 0; i <= name.length; i++) {" + " output = name.charAt(i) + output" + " }" + " return output;" + "}"); Invocable invokeEngine = (Invocable)engine; Console console = System.console(); for (Object name: args) { Object o = invokeEngine.invoke("reverse", name); console.printf("%s / %s%n", name, o); } } catch (NoSuchMethodException e) { System.err.println(e); } catch (ScriptException e) { System.err.println(e); } } } Running this program involves passing multiple strings via the command-line arguments. Each one passed along the command line will be displayed in both a forward and backward fashion. > java InvocableTest one two three one / eno two / owt three / eerht Caution There are two invoke() methods of Invocable. Sometimes the arguments can be ambiguous, and the compiler can t determine which of the two methods to use, as they both accept a variable number of arguments. In Listing 9-5, the enhanced for loop said each element was an Object, even though we knew it to be a String. This was to appease the compiler without adding a casting operation.
Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Tomcat Web Hosting services