import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class DynamicCodeExecution {
    public static void main(String[] args) {
        // 创建 ScriptEngineManager
        ScriptEngineManager manager = new ScriptEngineManager();
        
        // 获取 JavaScript 引擎
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        
        // 要执行的字符串代码
        String code = "var a = 10; var b = 20; a + b;";
        
        try {
            // 执行代码并获取结果
            Object result = engine.eval(code);
            System.out.println("执行结果: " + result);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}