From 7b0d778e82662a3b5bddd62c85e3d695903e9872 Mon Sep 17 00:00:00 2001 From: youys <1272586223@qq.com> Date: Thu, 17 Aug 2023 15:52:07 +0800 Subject: [PATCH] =?UTF-8?q?fix(DebuggerController)=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../educoder/debugger/DebuggerController.java | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/educoder/debugger/DebuggerController.java b/src/main/java/net/educoder/debugger/DebuggerController.java index 0f41d1b..ef8d547 100644 --- a/src/main/java/net/educoder/debugger/DebuggerController.java +++ b/src/main/java/net/educoder/debugger/DebuggerController.java @@ -55,19 +55,29 @@ public class DebuggerController { @RequestParam("fileOutPath") String fileOutPath, @RequestParam("originExecuteFile") String originExecuteFile) throws Exception { - // 编译代码 - compileCode(originExecuteFile); - - initDebuggerInstance(mainClass, classPath); - outputPath = fileOutPath; - debugger.setInputFile(fileInputPath); - debugger.setOutputFile(fileOutPath); - for (String breakPoint : breakPoints.split(",")) { - String[] segmentation = breakPoint.split(":"); - debugger.setBreakPoint(segmentation[0], Integer.valueOf(segmentation[1])); + try { + // 编译代码 + compileCode(originExecuteFile); + initDebuggerInstance(mainClass, classPath); + outputPath = fileOutPath; + debugger.setInputFile(fileInputPath); + debugger.setOutputFile(fileOutPath); + for (String breakPoint : breakPoints.split(",")) { + String[] segmentation = breakPoint.split(":"); + debugger.setBreakPoint(segmentation[0], Integer.valueOf(segmentation[1])); + } + debugger.startProgram(); + return getCurrentState(); + }catch (RuntimeException e){ + String errorMessage = e.getMessage(); + DebugResult result = new DebugResult(); + result.setIs_over(true); + result.setCurrent_line(-1); + result.setExpressions(new HashMap<>()); + result.setLocals(new HashMap<>()); + result.setOut(errorMessage); + return gson.toJson(result); } - debugger.startProgram(); - return getCurrentState(); } /** @@ -247,8 +257,11 @@ public class DebuggerController { */ 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); + String compileCommand = StringUtils.join("javac -g -cp ", compilePath, " ", compilePath+"*.java"); ShellResult shellResult = ShellUtil.executeAndGetExitStatus(compileCommand); logger.info("编译代码command:{}, status:{}, out:{}", compileCommand, shellResult.getExitStatus(), shellResult.getOut()); + if(shellResult.getExitStatus() != 0){ + throw new RuntimeException(shellResult.getOut().replaceAll("/data/workspace/", "")); + } } }