我在stackoverflow上找到了这段代码,但回复无法解决问题。我正在创建一个基于Web的编译器,我使用“name”字符串获取java文件名,使用“text”字符串获取代码。我正在使用Runtime.getRuntime()。exec()编译和执行代码。将在routput字符串中读取程序的输出,并在rerror字符串中读取错误流。现在,routput字符串显示为null,但是reerror strng显示:无法找到或加载主类C:\ Users \ NIHAR \ workspace2 \ MyFirstJavaProgram11.class。我甚至尝试在run命令中删除“.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
}
}
答案 0 :(得分:0)
在你的代码中有些东西是错误的,然后首先编辑你的代码 while((routput = stdInput.readLine())!= null){ 和 while((rerror = stdError.readLine())!= null){
null的位置你写“-1”并编译并朗读程序
在修改程序之后,你得到了一个错误的错误:无法找到或加载主类 然后你的程序没有加载jre然后在程序中设置你的jre。