进程运行时传递输入 [英] Process Runtime pass input

查看:197
本文介绍了进程运行时传递输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有rsync命令在java程序中运行...我面临的问题是rsync需要输入密码而我不知道如何将此密码传递给rsync命令才能工作?

I have rsync command to be run in a java program...the problem i am facing is that rsync requires a password to be entered and i am not understanding how to pass this password to the rsync command to work?

推荐答案

我要发布此代码示例:

Process rsyncProc = Runtime.exec ("rsync");
OutputStreanm rsyncStdIn = rsyncProv.getOutputStream ();
rsyncStdIn.write ("password".getBytes ());

Vineet Reynolds 领先于我。

正如Vineet Reynolds所指出的,使用这种方法需要额外的一段代码来检测rsync何时需要密码。所以使用外部密码文件似乎是一种更简单的方法。

As Vineet Reynolds pointed out using such approach will require an additional piece of code to detect when rsync requires a password. So using an external password file seems to be an easier way.

PS:可能存在与编码相关的问题,可以通过将字符串转换为a来解决使用适当的编码的字节数组这里

P.S.: There may be a problem related to the encoding, it can by solved by converting the string to a byte array using appropriate encoding as described here.

PPS:似乎我还没有评论答案,所以我不得不发布一个新答案。

P.P.S.: It seems that I can't yet comment an answer, so I had to post a new one.

这篇关于进程运行时传递输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆