代码如下:
Process exec = Runtime.getRuntime().exec("python3 " + pythonScriptPath); byte[] bytes = new byte[1024]; InputStream errorStream = exec.getErrorStream(); while (errorStream.read(bytes, 0, bytes.length) != -1) { String s = new String(bytes); System.out.println(s); }
此时可能会遇到依赖模块不存在的问题,而使用Runtime.getRunTime().exec(“conda activate xxx”)会遇到环境的问题。
解决方法如下:
在启动Java程序的用户下修改.bashrc,添加一行:
conda activate xxxx
其中xxxx为你的conda环境。之后source ~/.bashrc,再启动Java程序就可以调用了。
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/229834.html原文链接:https://javaforall.net
