-1

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
    }

}
  • I think you're trying to execute the program as a standard java application instead of a web application. This is why the java compiler looks for the main method but doesn't find it. Do you know how to run a web application? Most of people start with [tomcat](http://tomcat.apache.org/). – lateralus Feb 16 '15 at 10:12
  • I'm executing it as a web application. I've created a Dynamic Web Project in netbeans and it uses GlassFish Server. I'm using Google chrome to run the project – user23456677 Feb 28 '15 at 07:09

1 Answers1

0

in your code some thing is misscorrect then first of all edit your code while ((routput = stdInput.readLine()) != null) { and while ((rerror = stdError.readLine()) != null) {

The place of null you write "-1" and compile and rum the program

after this correction of a program then u got a errror like :Could not find or load main class then ur program not load a jre then set ur jre in program.