简体   繁体   English

如何输入从Java代码运行的终端命令的输入

[英]How to enter input to terminal command which is run from java code

I know, my English is not good. 我知道,我的英语不好。 Sorry for this. 非常遗憾。 So I try to explain with an example. 所以我尝试用一​​个例子来解释。

I have to do it for 5-6 examples and I will show you one of them. 我必须做5-6个示例,我将向您展示其中之一。 If anyone know how to do logic and answer me, it will be nice. 如果有人知道如何做逻辑并回答我,那将很好。

String cmd = "openssl enc -aes-256-cbc -e -in " + path + " -out " + path+"_E";
Process pb = Runtime.getRuntime().exec(cmd);
pb.waitFor();

If I enter this code in Terminal manually, it will ask me encryption password and I will be enter. 如果我在终端中手动输入此代码,它将要求我加密密码,然后将我输入。 After that, it ask me re-enter encryption password and I will be enter. 之后,它要求我重新输入加密密码,然后我将被输入。

What I want is that, can I handle both enter and re-enter things from java code ? 我想要的是,我可以处理java代码中的输入和重新输入吗? I want to enter automatically from java. 我想从Java自动输入。

PS: I do not want to disable to use password! PS:我不想禁用使用密码! I want enter from java code. 我想从Java代码输入。

Thanks for answers. 感谢您的回答。

You can handle it all through java. 您可以通过java处理所有内容。 See this post User input to the command line when using Runtime.getRuntime().exec(command); 使用Runtime.getRuntime()。exec(command);时,用户输入命令行。

As is mentioned in that post you can redirect the output stream of the process and send commands from Java using: 如该帖子所述,您可以重定向流程的输出流,并使用以下命令从Java发送命令:

out = p.getOutputStream();
out.write("fooUsername\n".getBytes());
out.flush();

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

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