-1

我在stackoverflow上找到了这段代码,但回复无法解决问题。我正在创建一个基于 Web 的编译器,其中使用“name”字符串获取 java 文件名,使用“text”字符串获取代码。我正在使用 Runtime.getRuntime().exec() 编译和执行代码。程序的输出将在 routput 字符串中读取,错误流将在 rerror 字符串中读取。现在 routput 字符串显示为空,但显示错误字符串:Could not find or load main class C:\Users\NIHAR\workspace2\MyFirstJavaProgram11.class。我什至尝试在运行命令中删除“.class”扩展名。请告诉我如何获取输出并使用 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
    }

}
4

1 回答 1

0

在你的代码中有些东西是不正确的,然后首先编辑你的代码 while ((routput = stdInput.readLine()) != null) { 和 while ((rerror = stdError.readLine()) != null) {

空的地方你写“-1”并编译和朗姆程序

在对程序进行此更正后,您会收到一个错误,例如:找不到或加载主类,然后您的程序未加载 jre,然后在程序中设置您的 jre。

于 2015-02-16T10:21:23.687 回答