This code I had found on stackoverflow but the replies could not solve the problem. I'm creating a web based compiler where I take the java file name using the "name" string and the code using the "text" string. I'm compiling and executing the code using Runtime.getRuntime().exec(). The Output of the program will be read in the routput string and the error stream would be read in the rerror string. Now the routput string is showing null but the reerror strng is showing:Could not find or load main class C:\Users\NIHAR\workspace2\MyFirstJavaProgram11.class. I even tried by removing ".class" extension in the run command. Please tell me how can I get the output and display it using the servlet.
package Org.ServletEx.Test;
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FileCreater
*/
@WebServlet("/ServletExample")
public class ServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ServletExample() {
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\\NIHAR\\workspace2\\" + 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\\NIHAR\\workspace2\\"
+ name + ".java";
FileWriter filewriter = new FileWriter(file, true);
filewriter.write(text);
filewriter.close();
try {
// Process d = Runtime.getRuntime().exec(" set path=\"C:\\Program Files\\Java\\jdk1.7.0_03\\bin\"");
Process p = Runtime.getRuntime().exec(
"javac C:\\Users\\NIHAR\\workspace2\\"
+ name + ".java");
Process c = Runtime.getRuntime().exec("java C:\\Users\\NIHAR\\workspace2\\" + name);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(c.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(c.getErrorStream()));
String routput="";
while ((routput = stdInput.readLine()) != null) {
//process readLine
response.getWriter().println(routput);
}
response.getWriter().println(routput);
String rerror;
while ((rerror = stdError.readLine()) != null) {
//process readLine
response.getWriter().println(rerror);
}
response.getWriter().println(rerror);
}
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
}
}