This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 40913
840
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Sheriff
Posts: 67619
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic