gpt4 book ai didi

java - 如何在运行时将 jar 添加到 classPatch 以解决编译后的类包导入问题

转载 作者:行者123 更新时间:2023-12-01 11:02:08 59 4
gpt4 key购买 nike

我制作了一个简单的Java编译器工具,它编译作为字符串传递的类,如果编译失败,结果将作为字符串重新调整(正常或编译错误)。它与一个仅导入 jre 中包含的包的类一起使用。当类导入未包含在 classPatch 中的包时,会发生编译错误 - 正如目前所料。问题是,当我检查导入外部包的类时,我想在运行时加载该包,以便编译器能够解析导入。我正在检查所有可能的解决方案,包括系统类加载器、上下文类加载器、所有可能的工具,例如 JCL classloader (除了 OSGI 容器,我不想使用它),但似乎没有什么能像我需要的那样工作。它只是返回类对象,您可以在其中获取它的实例并调用方法。那不是我需要的。这是我的编译器代码:

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Plugins compiler - checks if Java class is correct. If compilation is correct it returns "OK", otherwise it returns compiler error.
*/
public class PluginsCompiler
{

public static final File TEMP_DIR = new File("C://YourDir//");

public String compile(String javaSource) throws IOException
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

/*
* We create our own OutputStream, which simply writes error into String
*/
OutputStream output = new OutputStream()
{
private StringBuilder sb = new StringBuilder();

@Override
public void write(int b) throws IOException
{
this.sb.append((char) b);
}

@Override
public String toString()
{
return this.sb.toString();
}
};

/*
* We need to write file temporary before compiling it
*/
String classNameWithPath =
TEMP_DIR.getAbsolutePath().toString() + "\\" + this.getClassName(javaSource)
+ ".java";
File file = new File(classNameWithPath);

/*
* if file doesn't exists, then we create it
*/
if (!file.exists())
{
file.createNewFile();
}

try
{
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(javaSource);
bw.close();
compiler.run(null, null, output, classNameWithPath);
}
finally
{
for (File f : TEMP_DIR.listFiles())
{
f.delete();
}
}
return output.toString().length() == 0 ? "OK" : output.toString();
}

private String getClassName(String classSource)
{
Pattern patter = Pattern.compile("(class\\s)(\\w+)(\\s|\\\\n|\\{)");
Matcher matcher = patter.matcher(classSource);
String className = "";

if (matcher.find())
{
className = matcher.group(2);
}

return className;
}

public static void main(String[] args) throws IOException
{
String testSource = "package com.test;\n"
+ "import java.io.IOException;\n"
+ "public class ExternaPlugin\n"
+ "{\n"
+ " public void foo(String title, String message) throws IOException\n"
+ " {\n"
+ " System.out.println(title + \" \" + message);\n"
+ " }\n"
+ "}\n";

PluginsCompiler compiler = new PluginsCompiler();
System.out.println(compiler.compile(testSource));
}
}

例如,如果测试类如下所示:

import static org.junit.Assert.assertEquals;   
import org.junit.Test;

public class MyTests{

@Test
public void multiplicationOfZeroIntegersShouldReturnZero() {

// assert statements
assertEquals("10 x 0 must be 0", 0, 10 * 0);

}
}

由于未解析导入,编译将失败。所以我想加载包 junit-4.12.jar 这样编译器就会解析这个包。有什么办法可以做到吗?

最佳答案

您可以将修改后的类路径传递给编译器,如代码片段中所示。

File[] files = new File[]{new File("/path/to/source/MyTests.java")};

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

String classPath = System.getProperty("java.class.path");
String testpath = classPath + ";/path/to/lib/junit-4.10.jar";

List<String> options = new ArrayList<>();
options.addAll(Arrays.asList("-classpath", testpath));

try (StandardJavaFileManager filemanager = compiler.getStandardFileManager(
null, null, null)) {
Iterable fileObjects = filemanager.getJavaFileObjects(files);
JavaCompiler.CompilationTask task = compiler.getTask(
null, null, null, options, null, fileObjects);
task.call();
}

关于java - 如何在运行时将 jar 添加到 classPatch 以解决编译后的类包导入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260089/

59 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com