本文介绍了如何从java运行python脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个java应用程序并遇到了解析STDF的要求。

I have this java application and ran into a requirement to parse an STDF.

实际上我只需要获取stdf的FAR,MIR和MRR部分文件。在sourceforge上存在一个stdf4j,但由于缺少文档,我不能使用它。

Actually all I needed is to get the FAR,MIR and MRR sections of an stdf file. There exists a stdf4j on sourceforge but I can't use it that much due to lack of documentation.

决心使用用python编写的stdfparser并且是相对更直接,更容易完成工作,实际上我已经修改过以满足我的需求。

The resolve was to use the stdfparser which is written in python and is relatively more straightforward and gets the job done easier, which in fact I have already modified to suit my needs.

所以现在我只需要详细调用这个脚本并阅读用Java生成文件并继续使用现有的应用程序。

So now I just need to invoke this script verbosely and read the resulting file in Java and move on with the exisiting application.

使用时出现问题:

Process p = r.exec("cmd /c python parser.py sample_data.std.gz -v test.txt");

生成的文本文件始终为空。 (请注意,我已经在实际的命令行上运行了这个并成功检索了我需要的内容。)

the resulting text file is always blank. (Note that I already ran this on the actual command line and successfully retrieved the contents I needed.)

有什么我错过了,或者有没有更好的备择方案。 (我正在努力避免使用jython,因为我无法在生产环境中安装它。)

Is there anything I'm missing out, or are there any better alternatives. (I'm trying to avoid jython as I cannot install it's jar on the production environment.)

推荐答案

你应该打电话给 p.waitFor(); 以便您的Java程序在外部进程执行完之后才会继续运行。

You should perhaps call p.waitFor(); so that your Java program doesn't move on until after the external process has finished executing.

Update 1:还要确保你正在读取 p.getErrorStream() p.getInputStream()。如果不这样做可能会导致此过程停止。

Update 1: Also make sure you are reading all the bytes out of p.getErrorStream() and p.getInputStream(). Failure to do so may hang the process.

更新2:您可能还需要检查错误代码的进程返回代码。进程返回代码通过 waitFor()的返回值访问。

Update 2: You may also want to check the process return code for an error code. The process return code is accessed via waitFor()'s return value.

这篇关于如何从java运行python脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:57
查看更多