gpt4 book ai didi

java - 使用python与java程序交互

转载 作者:太空狗 更新时间:2023-10-29 12:20:47 31 4
gpt4 key购买 nike

<分区>

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

基本上我正在寻找的是,我想在使用 python 运行时与 java 程序交互,以便我可以访问它的输出并将输入传递给它。我设法使用 python 运行了一个 Java 程序。我想知道我可以在我的 python 程序中访问 java 程序的输出。例如。在java程序中:System.out.println("Enter no.");在 python 中,我应该能够将“Enter no”作为字符串获取,并将值从 python.a 传递给 java 程序

直到没有,我设法做到了: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 程序:

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

}
}

谢谢 :)

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com