想在java中执行python代码,在windows中执行没问题,得到返回值
放在ubuntu执行后python代码块没有被执行,也没报错
try {
String[] args = new String[2];”
args[0] = ”/usr/local/bin/python3.9”;
args[1] = ”/var/vvvvv/xxxxx/jwt.py";
log.info("command is being excuted :" + args[0] + args[1]);
proc = Runtime.getRuntime().exec(args);
// ProcessBuilder pb = new ProcessBuilder().inheritIO().command(args);
// proc = pb.start();
in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
token = line;
}
proc.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
无法执行到python代码块
改写下,你忽略了错误信息,看看在ubuntu上的错误信息是啥?你代码没啥问题
@SneakyThrows
public static void main(String[] g) {
try {
String[] args = new String[2];
args[0] = "/usr/bin/python3";
args[1] = "/Users/zhangyz/IdeaProjects/demo/src/test.py";
log.info("command is being excuted :" + args[0] + args[1]);
ProcessBuilder processBuilder = new ProcessBuilder(args);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "utf-8"));
String line = null;
while ((line = in.readLine()) != null) {
String token = line;
System.out.println(line);
}
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
想在java中执行python代码,在windows中执行没问题,得到返回值
放在ubuntu执行后python代码块没有被执行,也没报错
1、要看看ubuntu报的具体是什么错误
2、java在windows安装了python版本和ubuntu是否一致,还有没其他相关软件冲突
你的python脚本在ubuntu系统上能正常运行吗?直接在命令行下执行试试,看运行有没有错误。
重装
不知道你这个问题是否已经解决, 如果还没有解决的话: