You need a command to run the python script. I did not use jython. The script contains the todoist-api library. The script is executed by the command:

try { String cmd = "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"; Process p = Runtime.getRuntime().exec(cmd); } catch (IOException e){e.printStackTrace();} 

I get an exception -

 " java.io.IOException: Cannot run program "/home/kiryushin/projects/python/stm/venv/lib/stmtest1.py": error=13, ΠžΡ‚ΠΊΠ°Π·Π°Π½ΠΎ Π² доступС" 

Changed access parameters including via chmod -r 777. OC Ubuntu 18.04 lts.

  • And what will happen if: String cmd = "python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"; ? - Alex Yu
  • 1) And who is trying to execute the script? The same kiryushin or someone else? For example - cron . 2) It is necessary to check the rights not only of the stmtest1.py file itself, but of all the directories along its path. 3) Is there any shebang in the stmtest1.py file in the first line? - Sergey
  • @AlexYu Strange, but it helped, because A few days earlier I tried to run both via python and python3.6. Only the script is not executed, but probably this is a problem with another. Everywhere on the forums I saw mentions that several parameters need to be passed via String [] cmd = {"first message", "second message"}. It is necessary to make an example without third-party libraries to see if the script is launched or not. After that, if I help close the discussion. - Sergey Kiryushin
  • @ Sergey Kiryushin nothing strange. When you run a script in a terminal, the shell reads the shebang , gets the path to the interpreter from it, starts the interpreter, and passes it the script file. The rantaym JVM does not. - Sergey Gornostaev

2 answers 2

From the comments, it became clear that the launch problem was that the python interpreter was not found.

A single chmod +x [имя скрипта] not enough to make a file executable. More precisely: correctly executed .

Solution options

1. chmod +x and shebang

According to the classics, you need to add the first line in the "shebang" script: #!python or #!/usr/bin/python or #!//usr/bin/env python or even #p!ipenv run

The main thing is to remember that the environment created by Runtime.getRuntime() may differ from what can be created in bash / zsh / fish / etc and check $PATH , $PYTHONPATH and so on.

2. Specify the cmd interpreter directly

Modify the script launch command without relying on shebang and specify the interpreter directly:

 String cmd = "python /home/kiryushin/projects/python/stm/venv/lib/stmtest1.py"; 

Check again the environment variables created by Runtime.getRuntime.

Interpreter:

  • for simple cases, just python
  • for cases it is more difficult to create virtualenv and start with the environment initialization command. My recommendation is to use pipenv and run c pipenv run [имя скрипта]

    Unknowingly, during the creation of the python project I forgot that I indicated the creation of a virtual environment and therefore, after running the script from Runtime, I could not pick up the todoist-api library. That's why I am:

    1. Recreated the project without creating a virtual environment (venv)
    2. Pointed in the manual interpreter and added shebang
    3. Using pip3 added the library todoist-api
    4. In the script call indicated:

      String cmd = "python3.6 /home/kiryushin/projects/python/stm2/stm.py"; Process p = Runtime.getRuntime (). Exec (cmd);

    • The point is to add your own answer - only if all other answers are not satisfactory, and the correct one is found independently. And then you need to publish: your own answer and take it as a decision. Otherwise - it is illogical - Alex Yu
    • @AlexYu The answer above is more complete there is a solution with preservation of the virtual environment. He wrote his version as an alternative, if something does not work out - Sergey Kiryushin