如何使用python执行java程序,同时考虑输入和输出 [英] How to execute java program using python considering inputs and outputs both

查看:237
本文介绍了如何使用python执行java程序,同时考虑输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我的python程序视为input.py

Consider my python program as input.py

import os.path,subprocess
from subprocess import STDOUT,PIPE

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]
proc = subprocess.Popen(cmd,stdout=PIPE,stderr=STDOUT)
input=subprocess.Popen(cmd,stdin=PIPE)
print proc.stdout.read()

我使用的Java文件是Hi.java

Java file I am using is Hi.java

import java.util.*;
class Hi
{
       public static void main(String args[])
       {
            Scanner t=new Scanner(System.in);
            System.out.println("Enter any string");
            String str=t.nextLine();
            System.out.println("This is "+str);
            int a=5;
            System.out.println(a);
       }
}

当我拨打输入时。 execute_java(Hi.hava),输出输入字符串当我输入字符串时说Jon ,然后再次打印输出为输入字符串这是Jon,即它提供整个输出两次。由于代码打印proc.stdout,可能是由于python代码 input = subprocess.Popen(cmd,stdin = PIPE)而输出的第二个输出.read()
我希望它只打印一次。我该怎么办?我希望我的python变量接收java程序的所有输出,并使用该变量我将在屏幕上显示输出。此外,如果有一个java程序的输入,我希望该用户输入将存储在我的python变量中的输入,并使用此变量,我想将输入传递给java程序。我该怎么办?

When I call input.execute_java(Hi.hava), the output is "Enter the string" and when I enter the string say "Jon", then again it prints the output as "Enter the string This is Jon" i.e. it is providing the entire output two times. Maybe one output due to the python code input=subprocess.Popen(cmd,stdin=PIPE) and second output due to the code print proc.stdout.read() I want it to get printed only once. What should I do? I want my python variable to receive all the output of java program and using that variable I will display the output on the screen. Also if there is an input to java program, I want that user to enter the input which will be stored in my python variable and using this variable, I want to pass the input to java program. What should I do?

推荐答案

你应该使用 沟通 而不是 stdout.read communication 的参数是子进程的输入。另外,当您实际上不希望shell执行命令时,传递 shell = True 是个坏主意。相反,将参数作为列表传递:

You should use communicate instead of stdout.read. The argument of communicate is the input to the subprocess. Also, it's a bad idea to pass shell=True when you don't actually want a shell to execute your command. Instead, pass the arguments as a list:

import os.path,subprocess
from subprocess import STDOUT,PIPE

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

def execute_java(java_file, stdin):
    java_class,ext = os.path.splitext(java_file)
    cmd = ['java', java_class]
    proc = subprocess.Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
    stdout,stderr = proc.communicate(stdin)
    print ('This was "' + stdout + '"')

compile_java('Hi.java')
execute_java('Hi.java', 'Jon')

这篇关于如何使用python执行java程序,同时考虑输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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