@PostMapping("/run") public String Run(@RequestBody Map<String,String> map) throws IOException, InterruptedException {
// 创建临时文件 File tempFile = new File(System.getProperty("java.io.tmpdir"), "test.py"); // 将前端传入的代码写入临时文件 writeToFile(map.get("code"), tempFile); try {
ProcessBuilder processBuilder = new ProcessBuilder("D:\\Java\\python3.6\\python.exe",tempFile.getAbsolutePath()); // 将错误流与输出流合并 processBuilder.redirectErrorStream(true); // 开启进程 Process process = processBuilder.start(); // 使用字符流返回命令行执行结果 BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK")); // 使用可变String类 拼接响应对象并返回 StringBuilder result = new StringBuilder(); // 声明接收变量 String line; // 获取文件流中数据 while ((line = reader.readLine()) != null) {
// 拼接响应对象 result.append(line).append("\n"); } // 获取程序的执行结果 这个方法会阻塞主线程等待命令行执行完毕后返回 int exitCode = process.waitFor(); // 运行成功 if (exitCode == 0) {
return result.toString(); } else {
return "Command execution failed with exit code: " + exitCode; } } catch (IOException | InterruptedException e) {
return "Error executing command: " + e.getMessage(); } } @PostMapping("/execute") public String executeCode(@RequestBody Map<String, String> map) {
try {
// 创建临时文件 File tempFile = new File(System.getProperty("java.io.tmpdir"), "test.java"); // 将前端传入的代码写入临时文件 writeToFile(map.get("code"), tempFile); // 编译代码 compileCode(tempFile); // 执行代码 return executeCompiledCode(); } catch (Exception e) {
return "Error executing code: " + e.getMessage(); } } private void writeToFile(String code, File file) throws IOException {
// 创建字符缓冲流 写入字符型数据 try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
// 写入数据 writer.write(code); } } private void compileCode(File file) throws IOException {
// 获取编译器 JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); // 获取文件管理器 StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null); // 创建编译任务 Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects(file); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, null, null, compilationUnits); // 执行编译 task.call(); fileManager.close(); } private String executeCompiledCode() throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, java.lang.reflect.InvocationTargetException {
// 获取类加载器 URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{
new File(System.getProperty("java.io.tmpdir")).toURI().toURL()}); // 加载类 Class<?> compiledClass = classLoader.loadClass("test"); // 执行 main 方法 Method mainMethod = compiledClass.getMethod("main", String[].class); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); PrintStream printStream = new PrintStream(outputStream); PrintStream originalOut = System.out; System.setOut(printStream); try {
// 执行方法 mainMethod.invoke(null, new Object[]{
new String[0]}); // 刷新系统输出流 System.out.flush(); // 返回响应对象流 return outputStream.toString(); } finally {
System.setOut(originalOut); } }
讯享网
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/97249.html