How to execute Python script from Java?

后端 未结 4 789
一个人的身影
一个人的身影 2020-11-30 04:06

I can execute Linux commands like ls or pwd from Java without problems but couldn\'t get a Python script executed.

This is my code:

4条回答
  •  春和景丽
    2020-11-30 05:07

    You cannot use the PIPE inside the Runtime.getRuntime().exec() as you do in your example. PIPE is part of the shell.

    You could do either

    • Put your command to a shell script and execute that shell script with .exec() or
    • You can do something similar to the following

      String[] cmd = {
              "/bin/bash",
              "-c",
              "echo password | python script.py '" + packet.toString() + "'"
          };
      Runtime.getRuntime().exec(cmd);
      

提交回复
热议问题