用python获取java输出

用python获取java输出,java,python,subprocess,Java,Python,Subprocess,基于我的最后一个问题,我正在尝试用python运行一些java并获得结果。到目前为止,我的代码如下所示: import subprocess a = subprocess.Popen(['java -Xmx1024m -jar ./maui-standalone-1.1-SNAPSHOT.jar run /data/models/term_assignment_model -v /data/vocabulary/nyt_descriptors.rdf.gz -f skos'], cwd=r'/

基于我的最后一个问题,我正在尝试用python运行一些java并获得结果。到目前为止,我的代码如下所示:

import subprocess

a = subprocess.Popen(['java -Xmx1024m -jar ./maui-standalone-1.1-SNAPSHOT.jar run /data/models/term_assignment_model -v /data/vocabulary/nyt_descriptors.rdf.gz -f skos'], cwd=r'/Users/samuelburke/Repositories/RAKE-tutorial/', shell=True, stdout=subprocess.PIPE)

out, err = a.communicate()

print out
java本身在命令行中运行时,会从某些文本中返回关键字列表,如下所示:

04 Jun 2015 12:49:10  INFO Vocabulary - --- Loading RDF model from the SKOS file...
04 Jun 2015 12:49:12  INFO Vocabulary - --- Building the Vocabulary index from the RDF model...
04 Jun 2015 12:49:12  INFO Vocabulary - --- Statistics about the vocabulary: 
04 Jun 2015 12:49:12  INFO Vocabulary -     498 terms in total
04 Jun 2015 12:49:12  INFO Vocabulary -     0 non-descriptive terms
04 Jun 2015 12:49:12  INFO Vocabulary -     0 terms have related terms
Keyword: Food 0.010580524344569287
Keyword: Theater 0.0022471910112359544
Keyword: Education and Schools 0.0022471910112359544
Keyword: Child Care 0.0022471910112359544
Keyword: Trees and Shrubs 0.0022471910112359544
Keyword: Sociology 0.0022471910112359544
Keyword: Wines 0.0022471910112359544
Keyword: Science and Technology 0.0022471910112359544
Keyword: Heart 0.0022471910112359544
Keyword: Evolution 0.0022471910112359544
但是,运行上述代码会返回以下结果:

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.Exception: Name of model required argument.
    at com.entopix.maui.StandaloneMain.runMaui(StandaloneMain.java:40)
    at com.entopix.maui.StandaloneMain.main(StandaloneMain.java:109)
    ... 6 more

这是我第一次做这样的事情,我对编程非常陌生,所以我想我可能是完全错误的。非常感谢您的帮助。干杯

尝试将
Popen(['java-Xmx1024m-jar…
更改为
Popen(['java'、'-Xmx1024m'、'-jar'、…
。将每个参数作为列表中的一个单独元素。@Kevin提出了一个很好的观点,可以将列表用作
Popen
参数,其中每个命令行参数都是一个单独的字符串元素,或者使用整个字符串代替该列表,如
Popen('java-Xmx1024m-jar…
。我尝试了一下,但输出与在命令行中运行
java-help
相同。根据您的建议,这就是当前的样子:
Popen(['java'、'-Xmx1024m'、'-jar'、'/maui-standalone-1.1-SNAPSHOT.jar'、'run'、'/data/models/term\u assignment\u model'、'-v'、'/data/词汇表/nyt\u descriptors.rdf.gz'、'-f'、'skos'])
,如果这是你的意思的话。我尝试了几个变体,但没有成功。