简体   繁体   中英

Maintaining session while executing shell commands using a java program

We are working on a requirement to execute shell commands from java swing UI.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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