调用JSONObject报错java.lang.NosuchMethodError

本文解决了一个常见的编程问题:使用JSONObject时遇到java.lang.NoSuchMethodError错误。通过更换使用com.alibaba.fastjson.JSONObject,成功解决了由jar包冲突导致的问题。

JSONObject 报 java.lang.NosuchMethodError解决思路

报错:

java.lang.NosuchMethodError:org.apache.commons.collections.map.ListOrderdMap:methodV not found 如下图:

如图所示,在我调用JSONObject(net.sf.json.JSONObject)时,向里面put值时出现了这个问题:

在这里插入图片描述

解决方法:调用com.alibaba.fastjson.JSONObject,就解决了

在这里插入图片描述

总结

java.lang.NosuchMethodError这种出错,基本上就是你的jar包冲突,尝试改变jar包,或者升级jar包(版本冲突)。
本文链接:
https://blog.youkuaiyun.com/y_yanghao/article/details/106497330

package com.yhl.aps.algorithm.data.command; import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.util.HashMap; import java.util.List; import java.util.Map; import com.yhl.aps.algorithm.data.service.impl.ScheduleLogProvider; import com.yhl.aps.algorithm.exception.AlgorithmRuntimeException; import com.yhl.aps.config.RamaxelConfiguration; import com.yhl.platform.rbac.SystemHolder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.yhl.aps.algorithm.data.param.BaseAlgorithmData; import com.yhl.aps.algorithm.data.service.impl.ExecutorPath; import com.yhl.aps.algorithm.data.service.impl.HandleAlgorithmData; import com.yhl.aps.algorithm.outut.data.OperationO; import com.yhl.aps.algorithm.outut.data.ResultFileO; import com.yhl.platform.common.exception.CustomRuntimeException; import com.yhl.platform.common.utils.IOUtils; import lombok.extern.slf4j.Slf4j; import javax.annotation.Resource; /** * @ClassName: AutoScheduleExecutor.java * @Description: TODO(排程执行器). * @Package: com.yhl.aps.algorithm.data.command * @since JDK 1.8 * @author jian.li@youhualin.com} * @Date: 2021年1229日 上午12:35:41 * @Copyright 悠桦林信息科技(上海)有限公司 */ @Slf4j @Component public class ScheduleExecutor { @Autowired RamaxelConfiguration configuration; @Resource private ScheduleLogProvider scheduleLog; public static void main(String[] args) { Long seconds = 7200l; String hhmmss = ""; if (seconds <= 0) { hhmmss = "00:00:00"; } long hour = seconds / (60 * 60); long min = (seconds - hour * 3600) / 60; long ss = seconds - (hour * 3600 + min * 60); String hourStr = String.valueOf(hour); String minStr = String.valueOf(min); String ssStr = String.valueOf(ss); if (hour < 10) { hourStr = "0" + hourStr; } if (min < 10) { minStr = "0" + minStr; } if (ss < 10) { ssStr = "0" + ssStr; } hhmmss = hourStr + ":" + minStr + ":" + ssStr; System.out.println(hhmmss); } public ResultFileO doSchedule(BaseAlgorithmData baseAlgorithmData) { long resultFileTime = System.currentTimeMillis(); Map<String, String> pathMap = new HashMap<>(); String executorId = baseAlgorithmData.getExecutorId(); //生成基础业务数据 HandleAlgorithmData handleAlgorithmData = new HandleAlgorithmData(); handleAlgorithmData.writeBaseData(baseAlgorithmData); pathMap.put("ams",configuration.getAms()); pathMap.put("ams_ssd",configuration.getAmsSsd()); pathMap.put("ams_single",configuration.getAmsSingle()); pathMap.put("ams_complete_machine",configuration.getAmsCompleteMachine()); pathMap.put("ams_hy",configuration.getAmsHy()); pathMap.put("ams_sdt",configuration.getAmsSdt()); String workspacepath = baseAlgorithmData.getWorkspacePath(); ResultFileO resultFileO = executor(pathMap.get(SystemHolder.getDatabase()), ExecutorPath.getWorkPath(workspacepath, executorId), "stdder.txt", ExecutorPath.getOutputPath(workspacepath, executorId), executorId); log.info("执行{}[{}]用时:{}", baseAlgorithmData.getScheduleType(), baseAlgorithmData.getExecutorId(), (System.currentTimeMillis() - resultFileTime)); return resultFileO; } private ResultFileO executor(String cmd, String workpath, String errorLog, String outputPath, String executorId) { Long currentTimeMillis = System.currentTimeMillis(); try { //调用算法命令 log.info("开始调用命令==============="); Process process = new ProcessBuilder(cmd).directory(new File(workpath)) .redirectError(new File(errorLog)).start(); try { Field pidField = process.getClass().getDeclaredField("pid"); pidField.setAccessible(true); scheduleLog.updateAlgorithmPid(executorId, pidField.getInt(process)); } catch (NoSuchFieldException | SecurityException ex) { log.warn("获取不到算法程序进程ID"); } try (BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));) { String s = null; while ((s = br.readLine()) != null) { } } if (process.waitFor() == 0) { log.info("run successfully. Took {} ms. executorId = {} ", (System.currentTimeMillis() - currentTimeMillis), executorId); return getOperationO(outputPath); } else { log.error("algorithm run failed, executorId = {}", executorId); throw new AlgorithmRuntimeException("算法运行失败 executorId is" + executorId); } } catch (Exception e) { log.error("ruleSchedule Exception",e); throw new CustomRuntimeException("算法运行失败"); } } private ResultFileO getOperationO(String outputPath){ File workspaceFile = new File(outputPath); if(!workspaceFile.exists()) { throw new CustomRuntimeException("算法运行失败,未生成结果文件"); } ResultFileO resultFileO = new ResultFileO(); String result = IOUtils.readFile2String(outputPath + File.separator + "result_file.json"); JSONObject jsonObject = JSON.parseObject(result); List<OperationO> operationOs = (List<OperationO>)JSONArray.parseArray(jsonObject.getString("operations"), OperationO.class); resultFileO.setOperationOs(operationOs); List<String> cancelledOperations = (List<String>)JSONArray.parseArray(jsonObject.getString("cancelledOperations"), String.class); resultFileO.setCancelledOperations(cancelledOperations); return resultFileO; } }
10-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值