我正在运行Java程序,使用进程构建器调用Python进程,如下所示,
processBuilder = new ProcessBuilder(
Arrays.asList(
"/usr/bin/python",
"/opt/gui/oc_db5.py",
"-c",
"/opt/gui/test.json")
);
processBuilder.directory(new File("/opt/gui"));
processBuilder.start();
python程序的位置在/ opt / gui目录下,并且还有一个test.json文件也需要作为参数传递,使用" -c"选项,但我所看到的是系统正在附加带有JSON文件路径的java程序的位置,然后选择导致Python代码问题的.JSON文件。
python程序实际上得到的是/opt/java//opt/gui/test.json。我也尝试过../../但它没有使用test.json文件。
有没有办法可以指定.JSON文件作为python程序的参数?
答案 0 :(得分:1)
这似乎对我有用。我的意思是,它修复了目录问题。
try {
int exitCode = Runtime.getRuntime().exec("python /opt/gui/oc_db5.py -c /opt/gui/test.json", null, new File("/")).waitFor(); // run program and get exit code
} catch(Exception e) { // is there an error?
e.printStackTrace(); // print error
}