从tomcat访问classpath来动态编译代码片段

发布于 2024-12-25 06:23:01 字数 203 浏览 3 评论 0原文

我有动态生成的 java 代码片段,它们引用 tomcat 类路径中具有公共 api 的类。我能够通过反射正确识别这些类的公共 api(方法),但是在使用这些信息生成我的 java 代码之后,我将使用 Java 编译器 API 来编译它。

这可能吗?我是否需要获取 tomcat 用于加载 servlet 的类路径并将其传递给 Java 编译器 API?

安迪

I have java code snippets dynamically generated which refer back to classes with public apis in the tomcat classpath. I am able to correctly identify the public apis (methods) of these classes thru reflection, but then after using this information to generate my java code I was to compile it with the Java Compiler API.

Is this possible? Do I need to get the classpath that tomcat has used to load my servlet somehow and pass it to the Java Compiler API?

Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木森分化 2025-01-01 06:23:01

您必须传递类路径..此代码会在类路径中检索内容

List<String> options = new ArrayList<String>();
options.add("-classpath");
StringBuilder sb = new StringBuilder();
URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
for (URL url : urlClassLoader.getURLs()){
    sb.append(url.getFile().replace("%20", " ")).append(File.pathSeparator);
}
options.add(sb.toString());

您将选项传递给compiler.getTask

CompilationTask任务=compiler.getTask(writer,memoryManager,diagnostics,options,classes,compilationUnits );

You will have to pass the classpath.. This code does the classpath retrieve stuff

List<String> options = new ArrayList<String>();
options.add("-classpath");
StringBuilder sb = new StringBuilder();
URLClassLoader urlClassLoader = (URLClassLoader) Thread.currentThread().getContextClassLoader();
for (URL url : urlClassLoader.getURLs()){
    sb.append(url.getFile().replace("%20", " ")).append(File.pathSeparator);
}
options.add(sb.toString());

You pass the options to the compiler.getTask

CompilationTask task = compiler.getTask(writer, memoryManager, diagnostics, options, classes, compilationUnits);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文