从Java调用(的Tomcat6)作为子过程的Python [英] Calling Python from Java (Tomcat6) as sub-process

查看:429
本文介绍了从Java调用(的Tomcat6)作为子过程的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从java / tomcat6中web应用程序调用一个python脚本。我目前使用以下code:

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());

我真的不能看到从Python脚本catalinia.out文件中的任何输出,并使用适配器库像的Jython作为脚本依赖于需要Python的numpy的模块工作的几个机器学习库是不可能的。

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.

帮助?

推荐答案

的解释可能是下列之一(或更多):

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.

该命令未能推出,因为命令名称不正确;例如它不能 $ PATH 找到。

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

该命令试图从标准输入 FD ...但是你没有提供任何输入(还)。

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; e.g if you are using pathnames with embedded spaces, or other things that would normally be handled by the shell.

此外,由于这是蟒蛇,这可能是与特定于Python的环境变量的问题,当前的目录和/或正在执行该命令的有效用户。

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.

如何进行:


  1. 确定是否python命令实际上是开始。例如。 黑客中的写的东西在启动时的临时文件。

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

更改为使用的ProcessBuilder 来创建过程对象。这会给你过流以及如何处理它们的更多的控制。

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

了解正在发生的事情对孩子的过程标准错误。 (的ProcessBuilder使用户能够重定向到标准输出...)

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

这篇关于从Java调用(的Tomcat6)作为子过程的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆