/**
*
*/
package com.jhunter.bbs.controller;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author jixiaolong
*
*/
@Controller
public class CompileController {
@RequestMapping(value = "/compile", method = RequestMethod.GET)
public String gotoCompile() {
return "/compile.jsp";
}
@RequestMapping(value = "/result", method = RequestMethod.POST)
public String compile(@RequestParam("code") String main,
@RequestParam("input") String put, Model model,
HttpServletRequest request) throws Exception {
// String realPath = request.getSession().getServletContext()
// .getRealPath("/");
String realPath = "c:/";
String input = put.trim();
System.out.println(input);
System.out.println(realPath);
File uploadPath = new File(realPath, "compile");
uploadPath.mkdirs();
String imputPackage = "import java.applet.*;" + "import java.awt.*;"
+ "import java.beans.*;" + "import java.io.*;"
+ "import java.lang.*;" + "import java.math.*;"
+ "import java.net.*;" + "import java.nio.*;"
+ "import java.rmi.*;" + "import java.security.*;"
+ "import java.sql.*;" + "import java.text.*;"
+ "import java.util.*;";
String className = "Class" + System.nanoTime();
String code = imputPackage + "\n" + "public class " + className + "{"
+ main + "}";// 构造类
System.out.println(code);
PrintWriter writer = new PrintWriter(new FileWriter(new File(
uploadPath, className + ".java ")));
writer.println(code);
writer.close();
String line = "";
String tmp = "";
Process compile = Runtime.getRuntime().exec(
"javac -encoding UTF-8 \"" + realPath + "compile\\" + className
+ ".java\" ");
// 编译JAVA文件
BufferedReader comError = new BufferedReader(new InputStreamReader(
compile.getErrorStream(), "gbk"));// 获得编译错误结果
while ((tmp = comError.readLine()) != null) {
line = line + tmp + "\n";
}
if (line.equals("")) {
Process run = null;
try {
run = Runtime.getRuntime().exec(
"java -cp " + realPath + "compile\\ " + className);
if (input != null && input != "") {
OutputStream output = run.getOutputStream();
output.write((input).getBytes("GBK"));
output.flush();
output.close();
}
BufferedReader runRight = new BufferedReader(
new InputStreamReader(run.getInputStream(),"gbk"));// 获得运行正确结果
while ((tmp = runRight.readLine()) != null) {
line = line + tmp + "\n";
}
BufferedReader runErro = new BufferedReader(
new InputStreamReader(run.getErrorStream(), "gbk"));// 获得运行错误结果
while ((tmp = runErro.readLine()) != null) {
line = line + tmp + "\n";
}
} catch (IOException e) {
e.printStackTrace();
line = e.toString();
}
}
System.out.println(realPath);
model.addAttribute("result", line);
// File java = new File(uploadPath, className + ".java ");
// java.delete();
// File javaClass = new File(uploadPath, className + ".class ");
// javaClass.delete();
return "/result.jsp";
}
}
//jiaolongzhi 原创 采用spring mvc 实现简单的java在线编译系统
复杂的java程序肯定不行了。期望不要太高哦。
接受 表单提交的 java源代码 运行出结果在result.jsp显示。
可以检测出语法错误。
注意 输入的源代码 不要写类名和最外面的一对大括号。也不要导入任何包
系统会自动导入java所有的包。
因为类名是 系统自动生成的 生成的 java class文件保存在c:/compile
如果要接收 控制台输入的参数 请在点运行前输入 。单击compile.jsp中的例子按钮 可以测试这个功能。
- 1
- 2
前往页