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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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>>?
@KhalidMohammadAli, please refer to updates, might be of some help
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 :)

2 Comments

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
i guess the 'output' parameter will take the output of the process of executing the exe, not the exe window it self, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.