rhino
Rhino is an open-source implementation of JavaScript written entirely in Java.
It is typically embedded into Java applications to provide scripting to end users.
It is embedded in J2SE 6 as the default Java scripting engine.
https://developer.mozilla.org/en-US/docs/Rhino
Invoke JavaScript from Java example
1 import javax.script.Invocable;
2 import javax.script.ScriptEngine;
3 import javax.script.ScriptEngineManager;
4 import javax.script.ScriptException;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7
8 public class JSTest {
9
10 /**
11 * @param args
12 */
13 public static void main(String[] args) {
14 ScriptEngineManager sem = new ScriptEngineManager();
15 ScriptEngine engine = sem.getEngineByName("ECMAScript");
16 FileReader fr = null;
17 String path = "/tmp/test.js";
18 try {
19 fr = new FileReader(path);
20 } catch (FileNotFoundException e) {
21 System.out.println(String.format("File %s not found", path));
22 }
23
24 if (fr != null) {
25 try {
26 engine.eval(fr);
27 if (engine instanceof Invocable) {
28 Invocable iv = (Invocable) engine;
29
30 try {
31 System.out.println(iv.invokeFunction("isOdd", new Integer(4)));
32 System.out.println(iv.invokeFunction("isOdd", new Integer(5)));
33 } catch (NoSuchMethodException ex) {
34 ex.printStackTrace();
35 } catch (ScriptException ex) {
36 ex.printStackTrace();
37 }
38 }
39 } catch (ScriptException ex) {
40 ex.printStackTrace();
41 }
42 }
43 }
44
45 }
switch case example
- rhino -modules . -require test.js
require with just console needs the -modules . argument
uses CommonJS (-require) http://wiki.commonjs.org/wiki/Modules/1.1
test.js
1 dict = require('./xyz/modulex.js');
2 console = require('asd/console.js');
3
4 var caseA = ["a","e","i","o","u"];
5 var caseB = ["b","c","d","f","g"];
6 var caseC = ["h","j","k","l","m"];
7 var caseD = ["n","p","q","r","s","t","v","w","x","y","z"];
8
9 function check_if(string){
10 var letter = string[0];
11 if( caseA.indexOf(letter) >=0) return "A";
12 if( caseB.indexOf(letter) >=0) return "B";
13 if( caseC.indexOf(letter) >=0) return "C";
14 if( caseD.indexOf(letter) >=0) return "D";
15 }
16
17 function check_case2(string){
18 var letter = string[0];
19
20 switch(true){
21 case caseA.indexOf(letter) >=0: return "A";
22 case caseB.indexOf(letter) >=0: return "B";
23 case caseC.indexOf(letter) >=0: return "C";
24 case caseD.indexOf(letter) >=0: return "D" ;
25 }
26
27 }
28
29 function check_case(string){
30 var letter = string[0];
31 switch(letter){
32 case "a":
33 case "e":
34 case "i":
35 case "o":
36 case "u":
37 return "A";
38 case "b":
39 case "c":
40 case "d":
41 case "f":
42 case "g":
43 return "B";
44 case "h":
45 case "j":
46 case "k":
47 case "l":
48 case "m":
49 return "C";
50 default:
51 return "D";
52 }
53 }
54
55 console.log( check_if("adfgt") );
56 console.log( check_if("fdfgt") );
57 console.log( check_if("kdfgt") );
58 console.log( check_if("wdfgt") );
59 console.log("####");
60 console.log( check_case("adfgt") );
61 console.log( check_case("fdfgt") );
62 console.log( check_case("kdfgt") );
63 console.log( check_case("wdfgt") );
64 console.log("####");
65 console.log( check_case2("adfgt") );
66 console.log( check_case2("fdfgt") );
67 console.log( check_case2("kdfgt") );
68 console.log( check_case2("wdfgt") );
69 console.log("####");
70
71 console.log( JSON.stringify(dict) );
72 dict.info( dict.a );
xyz/modulex.js
asd/console.js