Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.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.getRuntime().exec()_Java_Process_Runtime - Fatal编程技术网

Java 执行后将参数发送到Runtime.getRuntime().exec()

Java 执行后将参数发送到Runtime.getRuntime().exec(),java,process,runtime,Java,Process,Runtime,我需要在java程序中执行一个命令,但在执行该命令后,它需要另一个参数(在我的例子中是密码)。如何管理Runtime.getRuntime().exec()的输出进程以接受参数以进一步执行 我尝试了newbufferedwriter(newoutputstreamwriter(signingProcess.getOutputStream()).write(“123456”)但它不起作用。如果在windows上工作,则应在windows命令中提供参数 有关详细信息,请访问此链接:您的程序是否没有-

我需要在java程序中执行一个命令,但在执行该命令后,它需要另一个参数(在我的例子中是密码)。如何管理
Runtime.getRuntime().exec()
的输出进程以接受参数以进一步执行


我尝试了
newbufferedwriter(newoutputstreamwriter(signingProcess.getOutputStream()).write(“123456”)但它不起作用。

如果在windows上工作,则应在windows命令中提供参数


有关详细信息,请访问此链接:

您的程序是否没有--password选项?通常所有基于命令行的程序都会这样做,主要用于脚本

Runtime.getRuntime().exec(new String[]{"your-program", "--password="+pwd, "some-more-options"});
或者更复杂、更容易出错的方式:

try {
    final Process process = Runtime.getRuntime().exec(
            new String[] { "your-program", "some-more-parameters" });
    if (process != null) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    DataInputStream in = new DataInputStream(
                            process.getInputStream());
                    BufferedReader br = new BufferedReader(
                            new InputStreamReader(in));
                    String line;
                    while ((line = br.readLine()) != null) {
                        // handle input here ... ->
                        // if(line.equals("Enter Password:")) { ... }
                    }
                    in.close();
                } catch (Exception e) {
                    // handle exception here ...
                }
            }
        }).start();
    }
    process.waitFor();
    if (process.exitValue() == 0) {
        // process exited ...
    } else {
        // process failed ...
    }
} catch (Exception ex) {
    // handle exception
}
此示例将打开一个新线程(请记住并发和同步),该线程将读取进程的输出。类似地,只要流程没有终止,您就可以向流程提供输入:

if (process != null) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                DataOutputStream out = new DataOutputStream(
                        process.getOutputStream());
                BufferedWriter bw = new BufferedWriter(
                        new OutputStreamWriter(out));
                bw.write("feed your process with data ...");
                bw.write("feed your process with data ...");
                out.close();
            } catch (Exception e) {
                // handle exception here ...
            }
        }
    }).start();
}
希望这有帮助

Runtime r=Runtime.getRuntime();
process p=r.exec("your string");

尝试这种方式

我尝试了new BufferedWriter(new OutputStreamWriter(signingProcess.getOutputStream())。write(“123456”)但没有起作用。问题是在进程开始执行后向其提供数据,不是命令行参数。@Dukeling:密码几乎可以作为参数传递。但如果执行后有其他选项,我会更满意:Dsure,执行后有一种更复杂的方法->我将扩展我的答案。