【发布时间】:2016-09-28 02:31:42
【问题描述】:
我正在尝试从 Java 执行 python 脚本。当我手动执行python脚本时,它可以正常工作。当我从 Java 执行它时,它的参数有问题:实际上,python 以“Usage”错误响应,就好像我没有传递参数一样。
Java 代码:
String pythonCommand="python /path/to/myscript.py --key='value list here' -otherparam=param";
p = Runtime.getRuntime().exec(pythonCommand);
String line;
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
p.waitFor();
logger.debug("Exit value: "+p.exitValue());
while ((line = input.readLine()) != null) {
logger.debug("Python command output: " +line);
}
while ((line = error.readLine()) != null) {
logger.debug("Python command ERROR output: " +line);
}
input.close();
我再说一遍,如果我只是复制粘贴并按字面意思执行“pythonCommand”字符串中的内容,则脚本可以正常工作,但如果我从 Java 执行它,它会说:
2016-05-30 09:46:32 [threadName] DEBUG - Executing command: python /path/to/myscript.py --key='value list here' -otherparam=param
2016-05-30 09:46:32 [threadName] DEBUG - Exit value: 1
2016-05-30 09:46:32 [threadName] DEBUG - Python command ERROR output: Usage:
2016-05-30 09:46:32 [threadName] DEBUG - Python command ERROR output: myscript.py [--key=<value list here>] --otherparam=<param>
2016-05-30 09:46:32 [threadName] DEBUG - Python command ERROR output: myscript.py (-h | --help)
2016-05-30 09:46:32 [threadName] DEBUG - Python command ERROR output: myscript.py --version
这里有什么问题?
编辑:另一个重要信息!如果我执行不带参数的 Python 脚本,例如“python /path/to/test.py”,它将完美运行。
edit2:我尝试执行一个不包含带有 ' 或 " 的多字参数的 Python 脚本,它起作用了。所以多字参数肯定是问题。我应该如何传递它们?
【问题讨论】:
标签: java python parameters parameter-passing