javapythonweb-servicesjspjython

How to write Python 3 code in a Java web code?


I am trying to write inside a JSP/Servlet Java web project, a python-3 Machine Learning code depends on torch and some advanced frameworks.

I tried:

  1. to use Jython but it did not work because it only works for python 2 programs.
  2. to use ProcessBuilder and Runtime.getRuntime().exec("pythonFile.py") to execute the python file but nothing worked.

Is there any suggestions on how to write that Python code in the Java project or communicate them?


Solution

  • For option #2, you'd need to pass the python executable into the exec method. For example:

    Runtime.getRuntime().exec("python pythonFile.py")
    

    Source: https://www.edureka.co/community/358/how-to-execute-a-python-file-with-few-arguments-in-java