-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
85 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package zimu.tests; | ||
|
||
import javax.script.Compilable; | ||
import javax.script.CompiledScript; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
|
||
public class Test2 { | ||
|
||
public static void main(String[] args) { | ||
ScriptEngineManager manager = new ScriptEngineManager(); | ||
ScriptEngine engine = manager.getEngineByName("JavaScript"); | ||
try { | ||
String script = "function getUrl(){var url='adsf';return url;} getUrl()"; | ||
Compilable compilable = (Compilable) engine; | ||
CompiledScript JSFunction = compilable.compile(script); | ||
Object result = JSFunction.eval(); | ||
System.out.println(result); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package zimu.util; | ||
|
||
import javax.script.Compilable; | ||
import javax.script.CompiledScript; | ||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
|
||
public class ExeJsUtil { | ||
|
||
/** | ||
* 执行js并返回结果 | ||
* @param jsStr | ||
* @return | ||
*/ | ||
public static String getJsVal(String jsStr) { | ||
ScriptEngineManager manager = new ScriptEngineManager(); | ||
ScriptEngine engine = manager.getEngineByName("JavaScript"); | ||
try { | ||
Compilable compilable = (Compilable) engine; | ||
CompiledScript JSFunction = compilable.compile(jsStr); | ||
Object result = JSFunction.eval(); | ||
return result != null ? result.toString() : null; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} |