AnyLogic跨Mac M2运行报错:IOUtils.byteArray方法不存在
解决方案:AnyLogic M2 Mac Excel读取commons-io兼容问题
一、替换AnyLogic内置的commons-io库
- 定位AnyLogic安装目录:Mac默认路径为
/Applications/AnyLogic[版本号]/AnyLogic.app/Contents/plugins/com.anylogic.engine_[版本号]/lib - 找到旧版
commons-io-*.jar,备份后删除 - 下载适配ARM架构的commons-io稳定版本(推荐2.11.0及以上,避免过新版本)
- 将新jar包放入上述lib目录,重启AnyLogic后重新运行模型
二、改用Java POI API直接读取(绕过内置库依赖)
如果替换库无效,可直接调用AnyLogic底层依赖的POI API,代码示例:
import org.apache.poi.ss.usermodel.*; import java.io.File; // 读取Excel文件 File excelFile = new File("你的Excel文件绝对/相对路径"); Workbook workbook = WorkbookFactory.create(excelFile); Sheet sheet = workbook.getSheet(airportName); int time = (int)time() - 15; String airportName = checkAptName(this); // POI行索引从0开始,对应原代码的行2(跳过表头行) for (int rowIndex = 1; rowIndex < sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) continue; // 原代码的列1对应POI的列0 Cell tstCell = row.getCell(0); int tstValue = (int)tstCell.getNumericCellValue(); if (tstValue == time) { // 原代码的列2对应POI的列1 Cell destCell = row.getCell(1); String destinationValue = destCell.getStringCellValue(); destinationApts.add(main.airports.get(getApt(destinationValue))); departingPed.inject(1); } } workbook.close();
注意:Mac上需使用正确的文件路径,避免Windows盘符格式。
三、升级AnyLogic版本
确认当前AnyLogic版本支持Apple Silicon架构,建议升级到2024.x最新版,官方已针对M系列芯片优化了依赖库,大概率直接解决兼容问题。
四、临时替代方案:改用CSV格式读取
将Excel另存为CSV,用AnyLogic原生readCsvFile函数读取,避免复杂的库依赖问题,代码示例:
List<List<String>> csvData = readCsvFile("你的CSV文件路径", ",", true); // true表示跳过表头 int time = (int)time() - 15; String airportName = checkAptName(this); // CSV第一列对应原Excel列1,第二列对应原Excel列2 for (int rowIndex = 0; rowIndex < csvData.size(); rowIndex++) { List<String> row = csvData.get(rowIndex); int tstValue = Integer.parseInt(row.get(0)); if (tstValue == time) { String destinationValue = row.get(1); destinationApts.add(main.airports.get(getApt(destinationValue))); departingPed.inject(1); } }
内容的提问来源于stack exchange,提问作者Fitz
相关产品推荐
相关产品推荐

