java在线调试

This commit is contained in:
youys 2023-07-11 16:31:04 +08:00
parent 259ec8fe89
commit c19f49d38a
5 changed files with 122 additions and 13 deletions

View File

@ -2,8 +2,14 @@ package net.educoder.debugger;
import com.google.gson.Gson;
import net.educoder.model.DebugResult;
import net.educoder.model.ShellResult;
import net.educoder.util.ShellUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -22,16 +28,13 @@ import java.util.Map;
@RestController
public class DebuggerController {
// public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src:/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java/part4/src/precompiled.jar";
// public static final String MAIN_CLASS = "__Driver__";
public static final String CLASS_PATHS = "/Users/youyongsheng/Desktop/ZQ/online-debug/evaluation/test-java";
public static final String MAIN_CLASS = "Debug";
// private static Debugger debugger = new Debugger(MAIN_CLASS, CLASS_PATHS);
private static Debugger debugger = null;
private static final Gson gson = new Gson();
private String outputPath;
private final Logger logger = LoggerFactory.getLogger(DebuggerController.class);
@GetMapping("/")
public String home() {
@ -44,12 +47,16 @@ public class DebuggerController {
* @return
* @throws Exception
*/
@GetMapping("/start-program")
public String startProgram(@RequestParam String classPath,
@RequestParam String mainClass,
@RequestParam String breakPoints,
@PostMapping("/start-program")
public String startProgram(@RequestParam("classPath") String classPath,
@RequestParam("mainClass") String mainClass,
@RequestParam("breakPoints") String breakPoints,
@RequestParam("fileInputPath") String fileInputPath,
@RequestParam("fileOutPath") String fileOutPath) throws Exception {
@RequestParam("fileOutPath") String fileOutPath,
@RequestParam("originExecuteFile") String originExecuteFile) throws Exception {
// 编译代码
compileCode(originExecuteFile);
initDebuggerInstance(mainClass, classPath);
outputPath = fileOutPath;
@ -199,7 +206,7 @@ public class DebuggerController {
return gson.toJson(result);
}
// result.setExpressions();
result.setExpressions(new HashMap<>());
result.setFilename(debugger.getLocationFilename());
result.setCurrent_line(debugger.getLocationLineNumber());
@ -232,4 +239,16 @@ public class DebuggerController {
}
}
}
/**
* 编译代码
*
* @param originalEvaluateFile 原始评测文件
*/
private void compileCode(String originalEvaluateFile) {
String compilePath = "/data/workspace" + File.separator + originalEvaluateFile.substring(0, originalEvaluateFile.lastIndexOf("/") + 1);
String compileCommand = StringUtils.join("javac -g -cp ", compilePath, " ", "/data/workspace", File.separator, originalEvaluateFile);
ShellResult shellResult = ShellUtil.executeAndGetExitStatus(compileCommand);
logger.info("编译代码command:{}, status:{}, out:{}", compileCommand, shellResult.getExitStatus(), shellResult.getOut());
}
}

View File

@ -26,7 +26,6 @@ public class StreamGobbler extends Thread {
int i;
while ((i = bufferedReader.read()) != -1) {
System.out.println(filepath+ " write---------"+(char)i);
fileWriter.write((char)i);
fileWriter.flush();
}

View File

@ -0,0 +1,35 @@
package net.educoder.model;
/**
* shell执行结果
*
* @author 威少
*/
public class ShellResult {
/**
* 退出码
*/
private Integer exitStatus;
/**
* 实际输出
*/
private String out;
public Integer getExitStatus() {
return exitStatus;
}
public void setExitStatus(Integer exitStatus) {
this.exitStatus = exitStatus;
}
public String getOut() {
return out;
}
public void setOut(String out) {
this.out = out;
}
}

View File

@ -0,0 +1,56 @@
package net.educoder.util;
import net.educoder.model.ShellResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public final class ShellUtil {
private static final Logger logger = LoggerFactory.getLogger(ShellUtil.class);
/**
* 执行shell命令并获取输出
*/
public static String execute(String command) {
return executeAndGetExitStatus(command).getOut();
}
/**
* 执行命令并获得输出以及退出码
*/
public static ShellResult executeAndGetExitStatus(String command) {
ShellResult result = new ShellResult();
StringBuilder out = new StringBuilder();
Integer exitStatus = -1;
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", command);
pb.redirectErrorStream(true);
try {
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
out.append(line);
out.append(System.getProperty("line.separator"));
}
exitStatus = process.waitFor();
} catch (Exception e) {
logger.error("执行shell出错, command:{}", command, e);
}
result.setOut(out.toString().trim());
result.setExitStatus(exitStatus);
logger.debug("execute shell command: {}, out: {}, status: {}", command, out, exitStatus);
return result;
}
}

View File

@ -1,2 +1,2 @@
server:
port: 8083
port: 8080