Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
使用getOutputStream()为java中的进程提供输入_Java_Input_Process_Runtime.exec - Fatal编程技术网

使用getOutputStream()为java中的进程提供输入

使用getOutputStream()为java中的进程提供输入,java,input,process,runtime.exec,Java,Input,Process,Runtime.exec,我正在尝试创建一个类似控制台的java应用程序,以便使用Runtime.getRuntime.exec()运行其他java程序。流程的输入将从JTextField中给出。但流程在给出输入之前正在运行。如何使进程等待文本字段的输入 package org.peditor; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import ja

我正在尝试创建一个类似控制台的java应用程序,以便使用Runtime.getRuntime.exec()运行其他java程序。流程的输入将从JTextField中给出。但流程在给出输入之前正在运行。如何使进程等待文本字段的输入

package org.peditor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class RunProg extends JFrame implements ActionListener {


private static final long serialVersionUID = 1L;
private String line="",editorString="";
final JTextField intext = new JTextField(25);
JTextArea outtext = new JTextArea(20,40);

public RunProg (String fileName){
    JPanel cp = new JPanel(new BorderLayout());
    intext.addActionListener(this);
    outtext.setEditable(false);
    intext.setEditable(true);
    JScrollPane sp = new JScrollPane(outtext);
    cp.add(sp,BorderLayout.WEST);
    cp.add(intext,BorderLayout.SOUTH);
    this.setContentPane(cp);
    this.setVisible(true);
    this.pack();
    this.setLocationRelativeTo(null);
    this.setTitle("*************OUTPUT CONSOLE***********");
    try
    {
        Runtime r = Runtime.getRuntime();
        Process p = null;
        //Stripping the file extension
        fileName = fileName.substring(0, fileName.lastIndexOf("."));            
        p = r.exec("java "+fileName);
        BufferedReader inputStream = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader errStream = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        OutputStream childin = p.getOutputStream();
        BufferedWriter childWriter = new BufferedWriter(new OutputStreamWriter(childin));
        childWriter.write(getInput());
        childWriter.newLine();
        childWriter.flush();
        childin.close();

        while ((line = inputStream.readLine())!=null) 
        {
            editorString = editorString+line+"\n";
            outtext.append(editorString);
        }
        while ((line = errStream.readLine())!=null)
        {
            editorString = editorString+line+"\n";
            outtext.append("ERROR"+"\n"+editorString);
        }
        p.waitFor();
        inputStream.close();
        errStream.close();
    }
    catch(Exception e1)
    {
        outtext.setText("Error running the program  "+e1.getMessage());
    }

}
public void actionPerformed(ActionEvent e) {
    getInput();

}
private String getInput() {
    String s = intext.getText();
    intext.setText("");
    outtext.append(s+"\n");
    return s;
}

}您正在调用构造函数中的exec方法。显然,这是行不通的。您还执行了一个完全不起作用的随机操作。从构造函数中删除所有运行时内容,添加JButton,向该JButton添加actionListener,并在单击该按钮时执行运行时代码。

进程启动时是否应接受输入?还是它运行并侦听输入?“使用Runtime.getRuntime.exec()”嘘。加入我们这个千年&使用
ProcessBuilder
。更一般地说。阅读(并实施)的所有建议。这可能会解决问题。如果没有,它应该提供更多关于失败原因的信息。然后忽略它引用的
exec
,并使用
ProcessBuilder
构建
流程。还可以将
字符串arg
分解为
字符串[]args
,以说明本身包含空格的参数。