feat():测试

yuan
yuan 2024-09-05 21:05:20 +08:00
parent cfb1982f75
commit 3b9c9d0f6b
1 changed files with 38 additions and 21 deletions

View File

@ -3,12 +3,12 @@ package com.muyu.compile;
import com.muyu.common.core.domain.Result;
import com.muyu.constant.MethodSuffix;
import com.muyu.upload.ALiYunUpload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
@ -27,7 +27,7 @@ public class JavaFileCompile {
public static Result<Object> compile(String fileName){
try {
// try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String[] strings = {"-classpath","/home/lib","-verbose","-d", "/home/lib","home/"+fileName + MethodSuffix.JAVASUFFIX };
@ -40,27 +40,44 @@ public class JavaFileCompile {
System.out.println("编译失败");
}
// class文件路径
String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX;
log.info(externalClassFilePath);
Path path = Paths.get(externalClassFilePath);
String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf("/"));
URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
// 加载类
// 注意类名必须是完全限定名(包括包名)
Class<?> clazz = externalClassLoader.loadClassFromPath(path, "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX);
System.out.println(clazz);
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
try {
String classContent = "";
BufferedReader bufferedReader = new BufferedReader(new FileReader("/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX));
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
classContent += line;
}
bufferedReader.close();
log.info("文件存储:" + fileName + MethodSuffix.CLASSSUFFIX);
ALiYunUpload.uploadClassFiles(classContent,fileName + MethodSuffix.CLASSSUFFIX);
} catch (IOException e) {
e.printStackTrace();
}
// // class文件路径
// String externalClassFilePath = "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX;
// log.info(externalClassFilePath);
// Path path = Paths.get(externalClassFilePath);
//
// String externalClassDir = externalClassFilePath.substring(0, externalClassFilePath.lastIndexOf("/"));
//
// URL[] urls = new URL[]{new File(externalClassDir).toURI().toURL()};
//
// ExternalClassLoader externalClassLoader = new ExternalClassLoader(urls);
//
// // 加载类
// // 注意类名必须是完全限定名(包括包名)
// Class<?> clazz = externalClassLoader.loadClassFromPath(path, "/home/lib/com/muyu/generate/"+fileName+ MethodSuffix.CLASSSUFFIX);
// System.out.println(clazz);
//
// } catch (MalformedURLException e) {
// throw new RuntimeException(e);
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
return Result.success();
}