xxl-job以GLUE(Python)形式执行py出现以下错误
【java.io.IOException: Cannot run program "python": CreateProcess error=2, 系统找不到指定的文件。】
2023-12-20 19:05:28 [com.xxl.job.core.thread.JobThread#run]-[133]-[xxl-job, JobThread-21-1703070328894]
----------- xxl-job job execute start -----------
----------- Param: 2023-12-20 19:05:28 [com.xxl.job.core.handler.impl.ScriptJobHandler#execute]-[80]-[xxl-job, JobThread-21-1703070328894] ----------- script file:\xxx\xxl-job-logs\jobHandler3\gluesource\21_1702981105000.py ----------- 2023-12-20 19:05:28 [com.xxl.job.core.util.ScriptUtil#execToFile]-[112]-[xxl-job, JobThread-21-1703070328894] java.io.IOException: Cannot run program "python": CreateProcess error=2, 系统找不到指定的文件。 at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at com.xxl.job.core.util.ScriptUtil.execToFile(ScriptUtil.java:76) at com.xxl.job.core.handler.impl.ScriptJobHandler.execute(ScriptJobHandler.java:81) at com.xxl.job.core.thread.JobThread.run(JobThread.java:166) Caused by: java.io.IOException: CreateProcess error=2, 系统找不到指定的文件。 at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 6 more 2023-12-20 19:05:28 [com.xxl.job.core.thread.JobThread#run]-[179]-[xxl-job, JobThread-21-1703070328894]
----------- xxl-job job execute end(finish) -----------
----------- Result: handleCode=500, handleMsg = script exit value(-1) is failed 2023-12-20 19:05:28 [com.xxl.job.core.thread.TriggerCallbackThread#callbackLog]-[197]-[xxl-job, executor TriggerCallbackThread]
----------- xxl-job job callback finish.
[Load Log Finish]
过程:正常cmd运行没有问题。各种重新安装、配置环境,文件夹访问权限等都不行,使用以下代码执行一次脚本后就正常了。
String param = XxlJobHelper.getJobParam(); // 获取参数 logger.warn("---------------->>>>>>>>>>>>>> 启用脚本 <<<<<<<<<<<<<<<----------------"); try{ JSONObject jsonObject = JSON.parseObject(param); String pythonScriptPath = jsonObject.get("pythonScriptPath").toString(); String[] cmd = new String[2]; cmd[0] = pythonPath; // Python解释器路径 cmd[1] = pythonScriptPath; // Python脚本路径 ProcessBuilder pb = new ProcessBuilder(cmd); Process p = pb.start(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String ret; while ((ret = in.readLine()) != null) { //System.out.println(ret); // 输出Python脚本的执行结果 } p.waitFor(); // 等待进程结束 //执行结束,返回给xxjob的数据 XxlJobHelper.handleSuccess("脚本 -->> 执行成功"); return ReturnT.SUCCESS; }catch (Exception e){ erpLogger.error("执行脚本过程发生异常: {}" ,e); //执行结束,返回给xxjob的数据 XxlJobHelper.handleSuccess("脚本 -->> 发生异常!"); return ReturnT.FAIL; }
没想到原因,感觉还是xxl-job问题,不知道是否需要自己在xxl-job中配置环境。