简体   繁体   English

使用Java程序执行Shell命令时维护会话

[英]Maintaining session while executing shell commands using a java program

We are working on a requirement to execute shell commands from java swing UI. 我们正在研究从Java swing UI执行Shell命令的要求。

We need to execute one command to start the session, once the session is started we need to execute some common commands repeatedly. 我们需要执行一个命令来启动会话,一旦会话开始,我们就需要重复执行一些通用命令。

We are able to execute the commands using the below command. 我们可以使用以下命令执行命令。

Runtime.getRuntime().exec("setSession")
Runtime.getRuntime().exec("execute individual task");

this way, everytime we have to set the session and executing the individual tasks. 这样,每次我们必须设置会话并执行各个任务。

Is there any way to execute a command (like setSession) once and retain the session to execute the remaining commands? 是否可以执行一次命令(如setSession)一次并保留会话以执行其余命令?

If I got Your task right, You might want to start a shell on the underlying system, get it's input and output streams and issue Your commands there. 如果我正确完成了您的任务,则可能要在基础系统上启动外壳,获取其输入和输出流,然后在此处发出命令。 Though that makes You operation system dependent. 虽然这使您依赖于操作系统。 But commands You executing probably are anyway. 但是您执行的命令可能仍然是。 Something like: 就像是:

Process p = Runtime.getRuntime().exec("cmd.exe");
OutputStream outputStream = p.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));

InputStream inputStream = p.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

writer.write("dir \n");
writer.flush();

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

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