Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的Runtime.exec()挂起,因为它正在等待System.in的输入_Java_Python_Multithreading_Process_Runtime.exec - Fatal编程技术网

java中的Runtime.exec()挂起,因为它正在等待System.in的输入

java中的Runtime.exec()挂起,因为它正在等待System.in的输入,java,python,multithreading,process,runtime.exec,Java,Python,Multithreading,Process,Runtime.exec,我有以下简短的python程序“test.py” 我从下面的java程序“ProcessRunner.java”执行上述程序 在运行命令时 java ProcessRunner 我无法以正确的格式将值“n”传递给Python程序,java运行也会挂起。处理这种情况并将值从java程序内部动态传递给python程序的正确方法是什么?或在python 3中,将阻止在标准输入上等待新的以行结尾的输入,但是java程序不会发送任何内容 尝试使用getOutputStream()返回的流写入Python

我有以下简短的python程序“test.py”

我从下面的java程序“ProcessRunner.java”执行上述程序

在运行命令时

java ProcessRunner
我无法以正确的格式将值“n”传递给Python程序,java运行也会挂起。处理这种情况并将值从java程序内部动态传递给python程序的正确方法是什么?

或在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]) 

如果我理解正确,您希望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();
        }
    }
}
import java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
公共类ProcessRunner{
公共静态void main(字符串[]args){
试一试{
final Process=Runtime.getRuntime().exec(“/bin/sh”);
试一试(
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);
}
}
}捕获(IOE异常){
抛出新的运行时异常(e);
}
}
私有静态void forwardOneByte(最终InputStream InputStream,
最终输出流(输出流)
抛出IOException{

if(inputStream.available())在此处查看有关如何与python进程通信的信息。有关正确创建和处理进程的许多好提示,请参见。然后忽略它引用
exec
,并使用
ProcessBuilder
创建进程。还可以将
String arg
分解为
String[]args
来解释包含空格字符的路径。太好了!这就是我要找的:)如果外部程序是java程序而不是python程序,那么即使在我执行上述相同的过程后,它也会挂起。
import sys

if len(sys.argv) > 1:
    print int(sys.argv[1]) 
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();
        }
    }
}