<i id='jdqsH'><tr id='jdqsH'><dt id='jdqsH'><q id='jdqsH'><span id='jdqsH'><b id='jdqsH'><form id='jdqsH'><ins id='jdqsH'></ins><ul id='jdqsH'></ul><sub id='jdqsH'></sub></form><legend id='jdqsH'></legend><bdo id='jdqsH'><pre id='jdqsH'><center id='jdqsH'></center></pre></bdo></b><th id='jdqsH'></th></span></q></dt></tr></i><div id='jdqsH'><tfoot id='jdqsH'></tfoot><dl id='jdqsH'><fieldset id='jdqsH'></fieldset></dl></div>

  • <small id='jdqsH'></small><noframes id='jdqsH'>

  • <legend id='jdqsH'><style id='jdqsH'><dir id='jdqsH'><q id='jdqsH'></q></dir></style></legend>
    <tfoot id='jdqsH'></tfoot>

          <bdo id='jdqsH'></bdo><ul id='jdqsH'></ul>
      1. 如何使用python执行java程序同时考虑输入和输出

        时间:2024-08-24
          <tfoot id='fNHjb'></tfoot>

          <legend id='fNHjb'><style id='fNHjb'><dir id='fNHjb'><q id='fNHjb'></q></dir></style></legend>

            <small id='fNHjb'></small><noframes id='fNHjb'>

            1. <i id='fNHjb'><tr id='fNHjb'><dt id='fNHjb'><q id='fNHjb'><span id='fNHjb'><b id='fNHjb'><form id='fNHjb'><ins id='fNHjb'></ins><ul id='fNHjb'></ul><sub id='fNHjb'></sub></form><legend id='fNHjb'></legend><bdo id='fNHjb'><pre id='fNHjb'><center id='fNHjb'></center></pre></bdo></b><th id='fNHjb'></th></span></q></dt></tr></i><div id='fNHjb'><tfoot id='fNHjb'></tfoot><dl id='fNHjb'><fieldset id='fNHjb'></fieldset></dl></div>
                <bdo id='fNHjb'></bdo><ul id='fNHjb'></ul>

                    <tbody id='fNHjb'></tbody>
                  本文介绍了如何使用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);
                         }
                  }
                  

                  当我调用 input.execute_java(Hi.hava) 时,输出是 "Enter the string",当我输入字符串时说 "Jon",然后它再次将输出打印为输入字符串This is Jon",即它提供了两次完整的输出.可能是由于 python 代码 input=subprocess.Popen(cmd,stdin=PIPE) 导致的一个输出和由于代码 print proc.stdout.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?

                  推荐答案

                  你应该使用 communicate 而不是 stdout.read.communicate 的参数是子进程的输入.此外,当您实际上不希望 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程序同时考虑输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:异步读取串行端口升压 下一篇:当我的 Java 应用程序退出/崩溃时,如何摆脱 Java 子进程?

                  相关文章

                  <tfoot id='twrEX'></tfoot>
                    <bdo id='twrEX'></bdo><ul id='twrEX'></ul>
                  <i id='twrEX'><tr id='twrEX'><dt id='twrEX'><q id='twrEX'><span id='twrEX'><b id='twrEX'><form id='twrEX'><ins id='twrEX'></ins><ul id='twrEX'></ul><sub id='twrEX'></sub></form><legend id='twrEX'></legend><bdo id='twrEX'><pre id='twrEX'><center id='twrEX'></center></pre></bdo></b><th id='twrEX'></th></span></q></dt></tr></i><div id='twrEX'><tfoot id='twrEX'></tfoot><dl id='twrEX'><fieldset id='twrEX'></fieldset></dl></div>

                  <legend id='twrEX'><style id='twrEX'><dir id='twrEX'><q id='twrEX'></q></dir></style></legend>

                      <small id='twrEX'></small><noframes id='twrEX'>