0

I have a java servlet running in a server, plus an 'exe file' located in the same server,

i want , in respond to the client passed parameters to the servlet , to run the exe file located on the server and show it to the client , even a screen shot,,

any ideas??!! please help

Khalid Dakeen
  • 23
  • 1
  • 4

2 Answers2

1

You can use Process and Runtime classes

Eg :

Runtime r = Runtime.getRuntime();    
Process p = r.getRuntime().exec("C:\\newfolder\\run.exe");  

For taking screenshot refer to how to take sc in java

This way you can save the image and then send this image to user.

For sending image to client refer to how to send file from sever to client

these are.the pieces , you need to put them together

UPDATE 1 : to kill the exe you can use p.destroy() ( not a good implementation though, as it forcefully kills the process)

UPDATE2 : to check if the process( which is executing your exe) hence to check if the exe is running or not, you can refer to how to check if a process is running

Community
  • 1
  • 1
Mukul Goel
  • 8,387
  • 6
  • 37
  • 77
  • thanx a lot , this helped, now i have two new problems: i) i have to make sure that the exe file is running to take a snap shot of it. ii) i have to close the exe file after taking the snap shot. any ideas>>? – Khalid Dakeen Nov 04 '12 at 21:07
  • @KhalidMohammadAli, please refer to updates, might be of some help – Mukul Goel Nov 04 '12 at 21:26
0

You can run an external command in Java by the following code:

Process p = Runtime.getRuntime().exec("your_external_program_here");

You can pass in parameters as well, simply amend the above line to include what parameters you want to pass into the program.

To retrieve the 'output' of the process you need to get the input stream for the process:

InputStream output = p.getInputStream();

Note the input stream is the piped output of the process. You can then view the contents (advisable to use a buffered reader) like this:

String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(output));
while ((line = reader.readLine()) != null) { ... }

Or alternatively you can look at ProcessBuilder which is easier to use :)

ramsinb
  • 1,985
  • 12
  • 17
  • thanx for responding,, after executing the file, I want to send the screen to the client, either life screen , or just a printscreen, i hope you got my point – Khalid Dakeen Nov 04 '12 at 20:39
  • i guess the 'output' parameter will take the output of the process of executing the exe, not the exe window it self, right? – Khalid Dakeen Nov 04 '12 at 21:08