简体   繁体   中英

Calling Python from Java (Tomcat6) as sub-process

I am trying to call a python script from a java/tomcat6 webapp. I am currently using the following code:

           Process p = Runtime.getRuntime().exec("python <file.py>"); 
       InputStream in = p.getInputStream();
       InputStreamReader isr = new InputStreamReader(in); 
       BufferedReader b = new BufferedReader(isr);

       logger.info("PYTHON OUTPUT");


       String line = null;
       while ( (line = b.readLine()) != null){
            logger.info(line);
       }

       p.waitFor();
       logger.info("COMPLETE PYTHON OUTPUT");
       logger.info("EXIT VALUE: "+p.exitValue());

I can't really see any output in the catalinia.out file from the python script and using an adapter library like jython is not possible as the script relies on several machine learning libraries that need python's Numpy module to work.

Help?

The explanation is probably one (or more) of following:

  • The command is failing and writing error messages to its "stderr" fd ... which you are not looking at.

  • The command is failing to launch because the command name is incorrect; eg it can't be found on $PATH .

  • The command is trying to read from its stdin fd ... but you haven't provided any input (yet).

  • It could be a problem with command-line splitting; eg if you are using pathnames with embedded spaces, or other things that would normally be handled by the shell.

Also, since this is python, this could be a problem with python-specific environment variables, the current directory and/or the effective user that is executing the command.


How to proceed:

  1. Determine if the python command is actually starting. For instance. "hack" the "" to write something to a temporary file on startup.

  2. Change to using ProcessBuilder to create the Process object. This will give you more control over the streams and how they are handled.

  3. Find out what is going to the child processes "stderr". (ProcessBuilder allows you to redirect it to "stdout" ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM