0

i am building a project in which i will ask the user to enter his code in the provided textarea and then on pressing the compile button,show the produced output on the next page.In the code ,using "String name", i am asking for the name of the class which user will use and using "String text", the code which user will enter .Firstly i compile the code using "javac class_name"(in the try block "Process p")and then execute it using "java class_name"(in the try block "Process c").Using this,a .java file and a .class file is created in the location "C:\Users\ANGADPENNY1\workspace\Project\src\".Now the question is that how can i display the produced output on the next page.

import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.JFileChooser;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.*;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static java.nio.file.StandardCopyOption.*;

/**
 * Servlet implementation class FileCreater
 */
@WebServlet("/FileCreater")
public class FileCreater extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public FileCreater() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */


protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Database Result";
    String docType = "<!doctype html public\"-//w3c//dt html 4.0"
            + "transitional//en\">\n";
    String name = request.getParameter("text1");

    String text = request.getParameter("TEXTAREA1");

    // String file = application.getRealPath("/new10.java");
    // String file = "JCQProject\\src\\"+name+".java";
    // String file = System.getProperty("user.home")+"\\Desktop\\"
    // +name+".java";

    try {

        File file = new File(
                "C:\\Users\\ANGADPENNY1\\workspace\\Project\\src\\" + name
                        + ".java");

        if (file.delete()) {
            System.out.println(file.getName() + " is deleted!");
        } else {
            System.out.println("Delete operation is failed.");
        }

    } catch (Exception e) {

        e.printStackTrace();

    }

    String file = "C:\\Users\\ANGADPENNY1\\workspace\\Project\\src\\"
            + name + ".java";
    FileWriter filewriter = new FileWriter(file, true);
    filewriter.write(text);

    filewriter.close();

    try {
        Process p = Runtime.getRuntime().exec(
                "javac  C:\\Users\\ANGADPENNY1\\workspace\\Project\\src\\"
                        + name + ".java");


        Process c = Runtime.getRuntime().exec(
                "java C:\\Users\\ANGADPENNY1\\workspace\\Project\\src\\"
                        + name);

    } catch (Exception e) {
        System.out.println(e.toString());
        e.printStackTrace();
    }


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    doGet(request, response);
    // TODO Auto-generated method stub
    }

}

2 Answers 2

1

You can get the output stream of the Process as follows -

Process p = Runtime.getRuntime().exec(
            "javac  C:\\Users\\ANGADPENNY1\\workspace\\Project\\src\\"
                    + name + ".java");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

Then you can read the Streams with

String readLine = null;
while ((readLine = stdInput.readLine()) != null) {
            //process readLine 
        }
Sign up to request clarification or add additional context in comments.

Comments

0

It's not easy with Runtime.exec, you have to be very careful with it. See this article for details.

Instead, I would reccomend using a ProcessBuilder. It has simple methods for redirecting both input and output (and don't forget about redirecting the error stream!).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.