1

I have been trying to solve this issue for 5 hours
when I try to execute the below code in the terminal it works fine. However, when I use the same code in flask application I am getting this error (sh: 1: java: not found)

import os 
environ = os.environ.copy()
os.putenv("JAVA_HOME", "/usr/lib/jvm/java-14-oracle/bin/java")
os.environ["PATH"] += os.pathsep + ":/usr/lib/jvm/java-14-oracle/bin/java/bin"
os.system("java -jar /home/../myproject/application/graphseg1.jar /home/../myproject/application/entire_text /home/.../myproject/application/segmented_text 0.40 2")

the above code works fine within the terminal as shown below: capture from terminal

However, when I try to run the same code within flask application, I am getting the following error: flask_app log

I am using Ubuntu 18.04, nginx,supervisor,gunicorn3
supervisor configration file:

[program:flask_app]
directory=/home/****/myproject
environment=PATH=/tmp/enter/envs/myenvi/bin
command=/tmp/enter/envs/myenvi/bin/gunicorn application:app --timeout 9223372036
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/flask_app/flask_app.err.log
stdout_logfile=/var/log/flask_app/flask_app.out.log
4
  • 3
    Can you print the value of os.environ["PATH"] just before the system call? Commented Jul 21, 2020 at 1:45
  • 3
    /usr/lib/jvm/java-14-oracle/bin/java/bin looks very fishy. bin/java/bin, really? Not just /usr/lib/jvm/java-14-oracle/bin? Keep in mind, the PATH should point to the directory that has java in it, not to the java executable itself; and you certainly can't add a bin subdirectory to an executable name and expect it to work. Commented Jul 21, 2020 at 2:11
  • @CharlesDuffy you are right. I misspelt the path. Thank you so much Commented Jul 22, 2020 at 0:48
  • Since this was caused by a typo, consider deleting the question if you're allowed (questions caused by topographic errors being #2 in the "some questions are still off-topic" list at stackoverflow.com/help/on-topic) Commented Jul 22, 2020 at 10:51

1 Answer 1

1

You should probably using Subprocess instead. It provides pipes for STDIN, STOUT, STDERR and the option for a full shell with some inherent security warnings.

See: https://docs.python.org/2/library/subprocess.html#module-subprocess

Seems like your path is not setup properly, like omajd said can you print it?

See also: Problems adding path and calling external program from Python

Sign up to request clarification or add additional context in comments.

2 Comments

While it's certainly the better practice, how would switching to subprocess solve this problem? And the OP is already telling us enough to know how and why their PATH is wrong; the necessary information was present in revision 1 of the question.
Also, having an upvoted answer prevents a low-reputation OP from deleting their own question, despite that question being off-topic (see #2 in the "some questions are still off-topic" list at stackoverflow.com/help/on-topic, and the last bullet point in the "Answer Well-Asked Questions" section at stackoverflow.com/help/how-to-answer, advising that questions that don't comply with the site's topicality rules shouldn't be answered).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.