JavaからPythonスクリプトを実行しようとしています。私は手動でPythonスクリプトを実行すると、正常に動作します。 Javaから実行すると、パラメータに問題があります。実際には、パラメータを渡さないかのように、Pythonは「Usage」エラーで応答します。JavaからのPythonスクリプトの実行 - パラメータ渡しの問題
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 /path/to/test.py"のようなパラメータなしでPythonスクリプトを実行すると、完全に動作します。
EDIT2:私は「や 'とのマルチワードのパラメータを含んでいて、それが働いていないのPythonスクリプトを実行しようとしたマルチワードパラメータは間違いなく、問題ですので、どのように私はそれらを渡す必要があり
申し訳ありませんが、動作しませんでした私はpythonCommand.splitを(」「)私が構築していないよので、使用していましたコマンド、それは私の関数に渡されました。 String [] commands = pythonCommand.split( ""); logger.debug( "Exe cutコマンド: "+コマンド); p = Runtime.getRuntime()。exec(コマンド); – user3804769
また、より多くのテストから、複数の単語のパラメータ(スペースを含む)が問題になっているように見えます。 – user3804769
例のように値リストに空白が含まれている場合( 'value list here')、単純なsplit()はあなたのコマンドを混乱させます。 logger.debug( "コマンドの実行:" +コマンド)の出力は何ですか? – Frank