2016-09-21 17 views
2
等待輸入

我有以下短Python程序「test.py」的Runtime.exec()在Java掛起因爲它是從System.in

n = int(raw_input()) 
print n 

我是從下面的Java執行上述程序節目 「ProcessRunner.java」

import java.util.*; 
import java.io.*; 

public class ProcessRunner { 
    public static void main(String[] args) { 
     try { 
      Scanner s = new Scanner(Runtime.getRuntime().exec("python test.py").getInputStream()).useDelimiter("\\A"); 
      System.out.println(s.next()); 
     } 

     catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

在運行命令,

java ProcessRunner 

我不能夠通過在親值 'N'每種格式的Python程序和Java運行掛起。什麼是正確的方式來處理這種情況,並從java程序中動態地將值'n'傳遞給python程序?

+0

看看這裏http://stackoverflow.com/questions/18903549/writing-to-inputstream-of-a-java-process關於如何與Python進程進行通信的信息。 –

+0

另請參閱[當Runtime.exec()不會](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html)很多很好的有關正確創建和處理過程的提示。然後忽略它是指'exec'並使用'ProcessBuilder'來創建進程。還要將'String arg'分解爲'String [] args'來解釋包含空格字符的路徑。 –

回答

1

raw_input()input()在Python 3中將阻止在標準輸入上等待新的行終止輸入,但是Java程序沒有發送任何東西。

嘗試使用由getOutputStream()返回的流寫入Python子流程。下面是一個例子:

import java.util.*; 
import java.io.*; 

public class ProcessRunner { 
    public static void main(String[] args) { 
     try { 
      Process p = Runtime.getRuntime().exec("python test.py"); 
      Scanner s = new Scanner(p.getInputStream()); 
      PrintWriter toChild = new PrintWriter(p.getOutputStream()); 

      toChild.println("1234"); // write to child's stdin 
      toChild.close();   // or you can use toChild.flush() 
      System.out.println(s.next()); 
     } 

     catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

另一種方法是通過n作爲命令行參數。這需要Python腳本的修改期望和處理命令行參數,以及Java代碼發送說法:

import java.util.*; 
import java.io.*; 

public class ProcessRunner { 
    public static void main(String[] args) { 
     try { 
      int n = 1234; 
      Process p = Runtime.getRuntime().exec("python test.py " + n); 
      Scanner s = new Scanner(p.getInputStream()); 
      System.out.println(s.next()); 
     } 

     catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 

而Python腳本,test.py

import sys 

if len(sys.argv) > 1: 
    print int(sys.argv[1]) 
+0

完美!這就是我一直在尋找:) – saint1729

+0

如果外部程序是一個java程序而不是python程序,那麼即使遵循上述相同的程序,它也會掛起。 – saint1729

0

如果我沒有理解你正確地想讓你的java程序將你的python腳本的任何輸出傳遞給System.out,並把你的java程序中的任何輸入傳遞給你的python腳本,對嗎?

看看下面的程序來了解你如何做到這一點。

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class ProcessRunner { 

    public static void main(String[] args) { 
     try { 
      final Process process = Runtime.getRuntime().exec("/bin/sh"); 

      try (
        final InputStream inputStream = process.getInputStream(); 
        final InputStream errorStream = process.getErrorStream(); 
        final OutputStream outputStream = process.getOutputStream() 
      ) { 

       while (process.isAlive()) { 
        forwardOneByte(inputStream, System.out); 
        forwardOneByte(errorStream, System.err); 
        forwardOneByte(System.in, outputStream); 
       } 
      } 
     } catch (IOException e) { 
      throw new RuntimeException(e); 
     } 
    } 

    private static void forwardOneByte(final InputStream inputStream, 
             final OutputStream outputStream) 
    throws IOException { 
     if(inputStream.available() <= 0) { 
      return; 
     } 

     final int b = inputStream.read(); 
     if(b != -1) { 
      outputStream.write(b); 
      outputStream.flush(); 
     } 
    } 
} 

注意此代碼只是一個概念演示。它會吃掉你的CPU,並且不能應付更大的吞吐量。