Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks,
Keerthi Kumar N
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34839
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keerthi Kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

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

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

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

</form>
</body>
</html>


Servlet:
*******
package com.Demo;

import java.io.IOException;
import java.io.PrintWriter;

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
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String program = request.getParameter("program");
try
{
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.

Thanks,
Keerthi Kumar N


 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to save the contents of request parameter "program" to a file in a specific location, say "/path/to/my/directory/Program.java".

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/Program.java" (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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic