I want to start a .exe file from jsp button ie, when I click a start button on jsp page, it should start the exe file. I was able to write code in java with getruntime.exec() with main method and when I run the java code am able to start the .exe file and could view the result that the file is started in console. I am not sure how to call this java class into jsp and run the exe file from there. Could some one please help on this.
Asked
Active
Viewed 3,404 times
2 Answers
2
Firstly you need to be familiar with java servlets. Here's some basic steps:
1. Create a servlet to run the exe program
package mycompany;
public ExeRunnerServlet extends HttpServlet {
protected doGet(HttpServletRequest req, HttpServletResponse res) throws Exception {
String cmdToRunExe = //..
// Implement exe running here..
PrintWriter writer = res.getWriter();
writer.append("Command has been run");
}
}
This servlet only service GET http method. Search online for the code on how to run exe in Java, eg: https://stackoverflow.com/a/10686041/179630.
2. Map the servlet on you WEB-INF/web.xml deployment descriptor
<web-app>
<servlet>
<servlet-class>mycompany.ExeRunnerServlet</servlet-class>
<servlet-name>ExeRunnerServlet</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>ExeRunnerServlet</servlet-name>
<url-pattern>/exerunner</url-pattern>
</servlet-mapping>
</web-app>
This will map the above servlet into http://myhost/mywarname/exerunner
3. Link it from JSP
Finally on your JSP you can create a html link to above servlet that will execute your exe program. Assuming your jsp is located at http://myhost/mywarname/page.jsp
:
<a href="exerunner">Run exe command</a>
Few words of warnings
- Creating a single servlet and mapping them manually into web.xml is considered an obsolete approach. Consider newer web framework such as Spring MVC / JSF.
- Calling exe from java is often regarded as a bad approach, especially if process forking is involved. I've seen cases where jvm took 800mb memory and the whole process had to be temporarily duplicated just to invoke exe command. Consider other integration approach such as web services.
-
Thanks for your quick help. It helped me. Now I have another problem.Am using two buttons here one for start and other for stop. With the start button am able to start the exe but when I use stop its not stopping. below is my jsp code. – user2266817 Sep 03 '13 at 17:34
-
Does the program actually respond to terminate signal? Even if you terminate the caller thread if the exe program doesn't respond to the signal it will still run – gerrytan Sep 03 '13 at 22:27
-
Hi gerrytan, thanks so much for helping me. your code helped me so much. But now my requirement has changed. I have to browse a file and then run the .exe file with a button which should show me the content of the browsed file.Could you please guide me on this. – user2266817 Sep 04 '13 at 19:09
-
Thanks for the above code and it worked well. But the only problem is the .exe file gets started everytme if am refreshing the webpage. – user2266817 Sep 12 '13 at 16:51
-
Hi gerrytan your solution helped me for the start up and thanks for this.Its good that you gave me few words of warning, in that could you please suggest me how to integrate using webservice. I mean how I can do the same thing using webservice.Thanks in advance. – user2266817 Sep 26 '13 at 16:38
0
Change the main method to some other method like executeProgram and then call the method from jsp.

Anil Puliyeril
- 375
- 1
- 11