用于 Java 5 和 Java 6 的动态内存中 Java 代码编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/616532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:04:46  来源:igfitidea点击:

On-the-fly, in-memory java code compilation for Java 5 and Java 6

javacompiler-constructionruntimecompilationruntime-compilation

提问by Ran Biron

How can I compile java code from an arbitrary string (in memory) in Java 5 and Java 6, load it and run a specific method on it (predefined)?

如何从 Java 5 和 Java 6 中的任意字符串(在内存中)编译 Java 代码,加载它并在其上运行特定方法(预定义)?

Before you flame this, I looked over existing implementations:

在你提出这个之前,我查看了现有的实现:

  • Most rely on Java 6 Compiler API.
  • Those that don't, rely on tricks.
  • Yes, I checked out commons-jci. Either I'm too dense to understand how it works, or it just doesn't.
  • I could not find how to feed the compiler my current class path (which is quite huge).
  • On the implementation that worked (in Java 6), I could not find how to correctly load inner classes (or inner anonymous classes).
  • I'd quite like it if the entire thing was in-memory, as the thing runs on multiple environments.
  • 大多数依赖于 Java 6 Compiler API。
  • 那些没有的,依靠技巧。
  • 是的,我检查了 commons-jci。要么我太密集而无法理解它是如何工作的,要么就是没有。
  • 我找不到如何为编译器提供我当前的类路径(非常大)。
  • 在有效的实现上(在 Java 6 中),我找不到如何正确加载内部类(或内部匿名类)。
  • 如果整个东西都在内存中,我非常喜欢它,因为它可以在多个环境中运行。

I'm sure this has been solved before, but I can't find anything that looks even half-production quality on google (except jci, which, as I've said before, I haven't managed to use).

我确定这之前已经解决了,但是我在 google 上找不到任何看起来甚至是半生产质量的东西(jci 除外,正如我之前所说,我还没有设法使用)。

Edit:

编辑:

  • I looked over JavaAssist - I need inner classes, Java 5.0 language level support and compiling with the entire classpath. Also, I'd like to create new classes on the fly. I might be mistaken, but I couldn't find how to do this with JavaAssit.
  • I'm willing to use a file-system based solution (calling javac) but I don't know how to divine the classpath, nor how to later load the files (which are not in my classpath) with a special classloader that can be recycled for multiple invocations. While I do know how to research it, I'd prefer a ready solution.
  • 我查看了 JavaAssist - 我需要内部类、Java 5.0 语言级别支持并使用整个类路径进行编译。另外,我想即时创建新类。我可能弄错了,但我找不到如何使用 JavaAssit 来做到这一点。
  • 我愿意使用基于文件系统的解决方案(调用 javac),但我不知道如何确定类路径,也不知道以后如何使用特殊的类加载器加载文件(不在我的类路径中)为多次调用而回收。虽然我知道如何研究它,但我更喜欢现成的解决方案。

Edit2: For now, I'm content with BeanShell "evaluate". Apparently it does everything I need it to (get a string, evaluate it in the context of the 'current' classpath. It does miss some of Java 5 features, but it can use enums (not define) and compiled 'generic' (erased) classes, so it should be enough for what I want.

Edit2:现在,我对 BeanShell 的“评估”感到满意。显然它完成了我需要的一切(获取一个字符串,在“当前”类路径的上下文中对其进行评估。它确实错过了一些 Java 5 功能,但它可以使用枚举(未定义)并编译“通用”(已擦除) ) 类,所以它应该足以满足我的需求。

I don't want to mark the answer as accepted yet since I do hope for a better solution to come up.

我不想将答案标记为已接受,因为我确实希望出现更好的解决方案。

Edit3: Accepted the beanshell suggestion - it really works wonderfully.

Edit3: 接受了 beanshell 的建议——它真的非常好用。

采纳答案by Bill K

If you're not completely tied to compiling, solutions like Beanshell, groovy and the other scripting languages are easily embedded (in-fact, java has built-in support for plugging in a scripting language so your code doesn't even know what language the script is written in)

如果你没有完全依赖编译,像 Beanshell、groovy 和其他脚本语言这样的解决方案很容易嵌入(事实上,java 内置了对插入脚本语言的支持,所以你的代码甚至不知道是什么语言脚本是这样写的)

Beanshell should run any 100% java code IIRC, and I believe Groovy can run most java code--possibly all.

Beanshell 应该运行任何 100% 的 Java 代码 IIRC,我相信 Groovy 可以运行大多数 Java 代码——可能全部。

回答by Yuval Adam

JCI looks fine. This code snippet should be your base:

JCI 看起来不错。此代码片段应该是您的基础:

JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");

MemoryResourceReader mrr = new MemoryResourceReader();
mrr.add("resource name string", yourJavaSourceString.getBytes());

MemoryResourceStore mrs = new MemoryResourceStore();

CompilationResult result = compiler.compile(sources, mrr, mrs);

// don't need the result, unless you care for errors/warnings
// the class should have been compiled to your destination dir

Any reason this should not work?

任何原因这不应该工作?



Edit:编辑:添加了一个MemoryResourceStoreMemoryResourceStore将编译后的类输出发送到内存,如请求。

Also, setting javacsettings, like classpath in your case, can be done via setCustomArguments(String[] pCustomArguments)in JavacJavaCompilerSettingsclass.

此外,设置javac你的情况设置,如类路径,可以通过完成setCustomArguments(String[] pCustomArguments)JavacJavaCompilerSettings类。

回答by Bill Rawlinson

You might want to check out Janino as well.

您可能还想查看 Janino。

From their website:

从他们的网站:

Janino is a compiler that reads a JavaTM expression, block, class body, source file or a set of source files, and generates JavaTM bytecode that is loaded and executed directly. Janino is not intended to be a development tool, but an embedded compiler for run-time compilation purposes, e.g. expression evaluators or "server pages" engines like JSP.

Janino 是一个编译器,它读取 JavaTM 表达式、块、类体、源文件或一组源文件,并生成直接加载和执行的 JavaTM 字节码。Janino 并不是一个开发工具,而是一个用于运行时编译目的的嵌入式编译器,例如表达式评估器或像 JSP 这样的“服务器页面”引擎。

http://www.janino.net/

http://www.janino.net/

Im currently using it in a pretty large mission critical project and it works just fine

我目前在一个相当大的关键任务项目中使用它,它工作得很好

回答by Maurice Perry

Javassistmight interest you

Javassist可能会让您感兴趣

回答by Thorbj?rn Ravn Andersen

Run inside a web container like Tomcat and first generate a JSP page, and then invoke it.

在像 Tomcat 这样的 web 容器中运行,首先生成一个 JSP 页面,然后调用它。

This also allow you to get rid of the old class definitions by simply overwriting the JSP page instead of having your classloader slowly run full.

这也允许您通过简单地覆盖 JSP 页面而不是让您的类加载器缓慢运行来摆脱旧的类定义。

Is the "in-memory" requirement due to speed or due to not changing the code base?

“内存中”要求是由于速度还是由于不更改代码库?

回答by Sergey Ponomarev

ECJ Eclipse Java Compiler

ECJ Eclipse Java 编译器

Eclipse provides and uses its own compiler that is not javac

Eclipse 提供并使用自己的编译器,它不是 javac

  • The Eclipse compiler is used inside the IDE (Eclipse)
  • The Eclipse compiler can also be used as a pure batch compiler outside of Eclipse
  • Eclipse 编译器在 IDE (Eclipse) 内部使用
  • Eclipse 编译器还可以用作 Eclipse 之外的纯批处理编译器

Compile a source file

编译源文件

$ java -jar ecj-3.5.2.jar HelloWorld.java

$ java -jar ecj-3.5.2.jar HelloWorld.java