aspose file tools
The moose likes JSP and the fly likes How to execute a java program in a html/jsp page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to execute a java program in a html/jsp page?" Watch "How to execute a java program in a html/jsp page?" New topic

How to execute a java program in a html/jsp page?

Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105

I need to write one html/jsp page, where there will be an text area to write any java program and when i click on the execute button it should compile that program and print the ouput down there. Please help me to do this. How to compile a java program in the html page and print the output ?

Awaiting your reply

Keerthi Kumar N

Keerthi Kumar N
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 31683

Submit the textarea to the server when the user clicks compile. Then compile the program from your servlet. (You can call Runtime.exec to run javac) You run it on the server as well - be careful the user can't do anything dangerous to your server - and send it back to the webpage.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Keerthi Kumar
Ranch Hand

Joined: Apr 20, 2009
Posts: 105

Please find my programs below:
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Demo Page</title>
<form action="/CompileJavaProgramServlet" method="post">

Write a java program in the below given text area and click on Execute button:

<textarea rows="15" cols="50" name="program"></textarea><br><br>

<input type="submit" value="Execute">   
<input type="reset" value="Reset">


package com.Demo;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CompileJavaProgramServlet extends HttpServlet
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
PrintWriter out = response.getWriter();
String program = request.getParameter("program");
Process process = Runtime.getRuntime().exec("javac");

catch (Exception e)
out.println("Exception "+e);


Please guide me how to execute the java program entered in the jsp page in this servlet.

Keerthi Kumar N

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 62315

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42954
You'll need to save the contents of request parameter "program" to a file in a specific location, say "/path/to/my/directory/".

Then you need to pass that path, along with the directory where the class file should be stored to javac (started via Runtime.exec), like "javac -d /path/to/my/directory" /path/to/my/directory/" (assuming that the class is named "Program".

Then you can again use Runtime.exec to execute the program (assuming that it has a proper "main" method). Make sure to pass the correct classpath to "java", and to handle the streams properly, as described in the artcile "When Runtime.exec() won't".
I agree. Here's the link:
subject: How to execute a java program in a html/jsp page?