使用python与java程序交互[duplicate]

时间:2021-12-29 20:48:59

Possible Duplicate:
Calling Java app with “subprocess” from Python and reading the Java app output

可能重复:从Python中调用带有“子进程”的Java应用程序并读取Java应用程序输出

Basically what I am looking for is, I want to interact with java program while its running using python so I can access its output and pass input to it. I have managed to run a Java program using python. I want to know can i access the outputs of java program in my python program. For example. In java program: System.out.println("Enter no."); In python i should be able to get "Enter no" as string and also pass value to java program from python.a

基本上我要找的是,我想在java程序使用python运行时与它进行交互,这样我就可以访问它的输出并将输入传递给它。我已经设法使用python运行了一个Java程序。我想知道我能否在我的python程序中访问java程序的输出。为例。在java程序:system . out。println(“输入不。”);在python中,我应该能够获得“输入no”作为字符串,并将值从python传递给java程序

What I managed to do till no : Python program :

我一直在做的事情:Python程序:

import sys
import os.path,subprocess

def compile_java(java_file):
    subprocess.check_call(['javac', java_file])

def execute_java(java_file):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    subprocess.call(cmd, shell=False)            

def run_java(java_file):
    compile_java(java_file)
    execute_java(java_file)

Java Program :

Java程序:

import java.io.*;
import java.util.*;
class Hi
{
        public static void main(String args[])throws IOException
        {
        Scanner t=new Scanner(System.in);

        System.out.println("Enter any integer");
        int str1=t.nextInt();           
        System.out.println("You entered"+str1);     

        }
}

Thanx :)

谢谢:)

2 个解决方案

#1


1  

If all you need is to get the output from a non-interactive execution of your Java program, use subprocess.check_output instead of subprocess.call.

如果您需要的只是从Java程序的非交互式执行中获得输出,则使用子流程。check_output代替subprocess.call。

http://docs.python.org/library/subprocess.html

http://docs.python.org/library/subprocess.html

You need Python 2.7 or newer for check_output to be available.

需要使用Python 2.7或更新的check_output。

If you need to interact with the Java program, you can do so using Popen.communicate, where you can read the process's output and send stuff to its input using file descriptors.

如果您需要与Java程序交互,您可以使用popen .通信,在那里您可以读取进程的输出,并使用文件描述符将内容发送到它的输入。

You can also use the pexpect python library to automate this kind of interaction, pexpect abstracts a lot of the legwork involved in using Popen.communicate.

您还可以使用pexpect python库自动化这种交互,pexpect抽象了使用popen . communication所涉及的许多遗留工作。

Note that these techniques apply for any kind of executable you need your Python program to interact with, not just Java; as long as it uses stdin and stdout, using these calls should work for you.

注意,这些技术适用于您需要Python程序与之交互的任何可执行文件,而不仅仅是Java;只要它使用stdin和stdout,使用这些调用就应该对您有用。

#2


1  

The easiest way would be to use Jython, which is a complete Python implementation that runs in the JVM, and can interact with native Java code. But if you want to use CPython, and generally continue down the path you've sketched out above, you'll want to create a live Python Popen object that you can interact with. For example:

最简单的方法是使用Jython,这是一个在JVM中运行的完整的Python实现,可以与本地Java代码交互。但是,如果您想使用CPython,并且通常沿着上面概述的路径继续,您将希望创建一个可以与之交互的活动Python Popen对象。例如:

import sys
import os.path,subprocess

def compile_java(java_file):
    subprocess.check_call(['javac', java_file])

def execute_java(java_file):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    return subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

def run_java(java_file):
    compile_java(java_file)
    process = execute_java(java_file)
    for i in range(10):
        process.stdin.write(str(i) + "\n")

#1


1  

If all you need is to get the output from a non-interactive execution of your Java program, use subprocess.check_output instead of subprocess.call.

如果您需要的只是从Java程序的非交互式执行中获得输出,则使用子流程。check_output代替subprocess.call。

http://docs.python.org/library/subprocess.html

http://docs.python.org/library/subprocess.html

You need Python 2.7 or newer for check_output to be available.

需要使用Python 2.7或更新的check_output。

If you need to interact with the Java program, you can do so using Popen.communicate, where you can read the process's output and send stuff to its input using file descriptors.

如果您需要与Java程序交互,您可以使用popen .通信,在那里您可以读取进程的输出,并使用文件描述符将内容发送到它的输入。

You can also use the pexpect python library to automate this kind of interaction, pexpect abstracts a lot of the legwork involved in using Popen.communicate.

您还可以使用pexpect python库自动化这种交互,pexpect抽象了使用popen . communication所涉及的许多遗留工作。

Note that these techniques apply for any kind of executable you need your Python program to interact with, not just Java; as long as it uses stdin and stdout, using these calls should work for you.

注意,这些技术适用于您需要Python程序与之交互的任何可执行文件,而不仅仅是Java;只要它使用stdin和stdout,使用这些调用就应该对您有用。

#2


1  

The easiest way would be to use Jython, which is a complete Python implementation that runs in the JVM, and can interact with native Java code. But if you want to use CPython, and generally continue down the path you've sketched out above, you'll want to create a live Python Popen object that you can interact with. For example:

最简单的方法是使用Jython,这是一个在JVM中运行的完整的Python实现,可以与本地Java代码交互。但是,如果您想使用CPython,并且通常沿着上面概述的路径继续,您将希望创建一个可以与之交互的活动Python Popen对象。例如:

import sys
import os.path,subprocess

def compile_java(java_file):
    subprocess.check_call(['javac', java_file])

def execute_java(java_file):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    return subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

def run_java(java_file):
    compile_java(java_file)
    process = execute_java(java_file)
    for i in range(10):
        process.stdin.write(str(i) + "\n")