This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
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 ?
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.
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.
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".