简体   繁体   English

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

[英]Send Parameter To Runtime.getRuntime().exec() After Execution

I need to execute a command in my java program but after executing the command , it required another parameter ( a password in my case ). 我需要在我的java程序中执行一个命令,但在执行命令后,它需要另一个参数(在我的情况下是一个密码)。 how can I manage the output process of Runtime.getRuntime().exec() to accept parameter for further execution ? 如何管理Runtime.getRuntime().exec()的输出过程以接受参数以便进一步执行?

I tried new BufferedWriter(new OutputStreamWriter(signingProcess.getOutputStream())).write("123456"); 我尝试了new BufferedWriter(new OutputStreamWriter(signingProcess.getOutputStream())).write("123456"); but it did not work. 但它不起作用。

Does your program not feature a --password option ? 您的程序是否没有--password选项? Normally all command line based programs do, mainly for scripts. 通常,所有基于命令行的程序都主要用于脚本。

Runtime.getRuntime().exec(new String[]{"your-program", "--password="+pwd, "some-more-options"});

Or the more complicated way and much more error-prone: 或者更复杂的方式,更容易出错:

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
}

This sample opens a new thread (keep in mind concurrency and synchronisation) that's going to read the output of your process. 此示例打开一个新线程(请记住并发和同步),它将读取进程的输出。 Similar you can feed your process with input as long as it has not terminated: 类似的,只要没有终止,您就可以使用输入来处理您的流程:

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();
}

Hope this helps. 希望这可以帮助。

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

try this way 试试这种方式

You should give in parameter your windows command if you work on windows 如果你在windows上工作,你应该给你的参数你的Windows命令

visit this link for more details : http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html 有关更多详细信息,请访问此链接: http//docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM