Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.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:在我在linux/winm/c上运行的第一个命令之后再执行一个命令_Java - Fatal编程技术网

Java:在我在linux/winm/c上运行的第一个命令之后再执行一个命令

Java:在我在linux/winm/c上运行的第一个命令之后再执行一个命令,java,Java,我有一个从Java程序执行的命令,如:- String command="C:\\my\\abc.bat"; Process process = null; try { process = Runtime.getRuntime().exec(command); process.waitFor(); 现在,当从命令行运行此命令时,会要求输入密码。在这里也一样。我怎样才能写出这个的密码 例如:- 1) 运行命令。 2) 输入密码:- 我已经完成了第一步。如何在密码执行后输入

我有一个从Java程序执行的命令,如:-

String command="C:\\my\\abc.bat";
Process process = null;
try {           
process = Runtime.getRuntime().exec(command); 
process.waitFor();
现在,当从命令行运行此命令时,会要求输入密码。在这里也一样。我怎样才能写出这个的密码

例如:- 1) 运行命令。 2) 输入密码:-


我已经完成了第一步。如何在密码执行后输入密码。

使用此语法将密码作为CLI参数传递:

String password = "your password"
Runtime.getRuntime().exec(new String[]{"C:\\my\\abc.bat", password});
或者,如果您的程序是交互式的,则使用获取将密码写入其中的输出流。例如:

String password = "your password"
PrintWriter writer = new PrintWriter(process.getOutputStream());
writer.println(password);
writer.close();
请注意,这是一个简化的示例,您必须适当地管理异常。

使用以下方法解决此问题:-

String command="C:\\my\\abc.bat";
Process process = null;
try {           
process = Runtime.getRuntime().exec(command); 
 OutputStream out=process.getOutputStream();
 out.write("mypass".getBytes());
 out.flush();

还删除了导致流关闭的
waitFor()

尝试过。。不起作用:(…它没有引发异常,但应该在此之后生成a票证。哪个不提交密码是外部进程所需的唯一数据?您有任何方法转储输入吗?是的,密码是唯一的东西。我在这里观察到的另一件事是,当我添加getInputStrem以查看它是否显示时“输入密码”。即使是这个字符串也不会出现。奇怪的是,这是因为该命令带有一个参数。在.bat之后是它的密码。。所以它类似于“C:\\my\\abc.bat password”“。如果我拼写的密码不正确,我会在InputStream中打印错误消息。根据您的评论,我知道您需要一个参数。请检查更新的答案。”。