feat():测试

yuan
yuan 2024-09-02 19:48:38 +08:00
parent b62c181969
commit ae63ab2c0e
2 changed files with 27 additions and 10 deletions

View File

@ -74,15 +74,6 @@ public class OSSFileLoad {
ossClient.shutdown(); ossClient.shutdown();
} }
} }
System.out.println("----------------------------");
try {
String command = "javac -cp . " + ":/home/app.jar" + fileName;
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("----------------------------");
//对路径里的.java文件进行编译 //对路径里的.java文件进行编译
System.out.println("第一步"); System.out.println("第一步");
SourceCodeCompiler.javaCompilerPath("home/"); SourceCodeCompiler.javaCompilerPath("home/");

View File

@ -9,6 +9,7 @@ import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider; import javax.tools.ToolProvider;
import java.io.File; import java.io.File;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
public class SourceCodeCompiler { public class SourceCodeCompiler {
@ -61,7 +62,7 @@ public class SourceCodeCompiler {
//通过源文件获取到想要编译的java类源代码迭代器包括所有的内部类其中每一个类都是一个JavaFileObjects也被称为一个汇编单元 //通过源文件获取到想要编译的java类源代码迭代器包括所有的内部类其中每一个类都是一个JavaFileObjects也被称为一个汇编单元
Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(file); Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjects(file);
//生成编译任务 //生成编译任务
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, Arrays.asList("-d","home/"), null, javaFileObjects); JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, Arrays.asList("-d","home/","cp","/home/app.jar"), null, javaFileObjects);
//执行编译任务 //执行编译任务
task.call(); task.call();
}catch (Exception e){ }catch (Exception e){
@ -69,4 +70,29 @@ public class SourceCodeCompiler {
} }
} }
// try {
// // 初始化 fileManager此处省略假设您已有此步骤
//
// // 创建一个包含所有依赖 JAR 文件的类路径列表
// List<String> classpath = Arrays.asList(
// "/home/app.jar"
// );
//
// // 通过源文件获取到想要编译的 JavaFileObjects
// Iterable<? extends JavaFileObject> javaFileObjects = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(file));
//
// // 生成编译任务,包含类路径
// JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null,
// Collections.singletonList("-d"), null, classpath, javaFileObjects);
//
// // 执行编译任务
// boolean success = task.call();
// if (!success) {
// throw new RuntimeException("Compilation failed");
// }
// } catch (Exception e) {
// // 适当的错误处理
// e.printStackTrace(); // 或使用日志记录
// }
} }