如何在运行时编译和部署 java 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1064259/
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
How can I compile and deploy a java class at runtime?
提问by Yaneeve
I am in the process of writing a rule engine that performs simple assignments as determined by conditional constructs. It is a prerequisite of the project that the rules be in XML format. I have modeled my XML schema to resemble simple code blocks. I wish to parse the XML and to then transform it into Java code. I then wish to compile (and run) this code at runtime. Doing so would mean that my rule engine no longer acts as an interpreter but executes native Java Byte Code.
我正在编写一个规则引擎,该引擎执行由条件构造确定的简单分配。规则为 XML 格式是项目的先决条件。我已经将我的 XML 模式建模为类似于简单的代码块。我希望解析 XML,然后将其转换为 Java 代码。然后我希望在运行时编译(并运行)这段代码。这样做意味着我的规则引擎不再充当解释器,而是执行本机 Java 字节码。
I have figured out the parsing stage and more or less the Java code generation phase. I would now like to figure out the last phase - the compile at runtime phase.
我已经弄清楚了解析阶段和或多或少的 Java 代码生成阶段。我现在想弄清楚最后一个阶段 - 运行时阶段的编译。
Following this thread: Compile to java bytecode (without using Java)I have become aware of the following possible solutions:
遵循此线程:编译为 java 字节码(不使用 Java)我已经意识到以下可能的解决方案:
I would love a comparison of these as well as other suggestions for solving the Java compile at runtime phase.
我很想对这些进行比较,以及在运行时阶段解决 Java 编译的其他建议。
采纳答案by cletus
Use the Java 6 Compiler API.
回答by kwatford
You could transform it into Clojure code, and the Clojure compiler will turn it into bytecode for you.
您可以将其转换为 Clojure 代码,Clojure 编译器会为您将其转换为字节码。
回答by Nick Holt
Save yourself the hassle and use BeanShellas alluded to here Executing java code given in a text file.
省去麻烦并使用BeanShell,正如此处提到的Executing java code given in a text file。
What is BeanShell?
BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.
You can use BeanShell interactively for Java experimentation and debugging as well as to extend your applications in new ways. Scripting Java lends itself to a wide variety of applications including rapid prototyping, user scripting extension, rules engines, configuration, testing, dynamic deployment, embedded systems, and even Java education.
BeanShell is small and embeddable, so you can call BeanShell from your Java applications to execute Java code dynamically at run-time or to provide extensibility in your applications. Alternatively, you can use standalone BeanShell scripts to manipulate Java applications; working with Java objects and APIs dynamically. Since BeanShell is written in Java and runs in the same VM as your application, you can freely pass references to "live" objects into scripts and return them as results.
In short, BeanShell is dynamically interpreted Java, plus a scripting language and flexible environment all rolled into one clean package.
什么是豆壳?
BeanShell 是一个小型、免费、可嵌入的 Java 源代码解释器,具有对象脚本语言功能,用 Java 编写。BeanShell 动态执行标准 Java 语法并使用常见的脚本编写便利对其进行扩展,例如松散类型、命令和方法闭包(如 Perl 和 JavaScript 中的那些)。
您可以交互式地使用 BeanShell 进行 Java 实验和调试,以及以新的方式扩展您的应用程序。编写 Java 脚本适用于各种应用程序,包括快速原型设计、用户脚本扩展、规则引擎、配置、测试、动态部署、嵌入式系统,甚至 Java 教育。
BeanShell 小巧且可嵌入,因此您可以从 Java 应用程序调用 BeanShell 以在运行时动态执行 Java 代码或在应用程序中提供可扩展性。或者,您可以使用独立的 BeanShell 脚本来操作 Java 应用程序;动态处理 Java 对象和 API。由于 BeanShell 是用 Java 编写的,并且与您的应用程序在同一 VM 中运行,因此您可以自由地将“活动”对象的引用传递到脚本中并将它们作为结果返回。
简而言之,BeanShell 是一种动态解释的 Java,加上一种脚本语言和灵活的环境,都被整合到一个干净的包中。
回答by Silent Warrior
Groovy, BeanShell or any other scripting language which is based on JVM have such a facility to inject, modify, add and run code at runtime. Actually all the scripting language are interpreted, so actually those are not compiling at runtime.
Groovy、BeanShell 或任何其他基于 JVM 的脚本语言都具有在运行时注入、修改、添加和运行代码的功能。实际上所有的脚本语言都是解释性的,所以实际上它们不是在运行时编译的。
回答by Rahul Garg
you can fork a process like this
你可以分叉这样的过程
Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);
//you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
log.debug("PROCESS outputstream : " + p.getInputStream() );
log.debug("PROCESS errorstream : " + p.getErrorStream());
p.waitFor(); // Wait till the process is finished
and can compile and run it.
并且可以编译和运行它。
回答by Ben F Rayfield
Javassist is almost a full Java compiler written in Java, and it's completely made of Java. You can't give it a whole .java file at once, but you can give it the code string for individual functions and add them to the same CtClass object, which becomes bytecode and then a java.lang.Class.
Javassist 几乎是一个用 Java 编写的完整 Java 编译器,它完全由 Java 组成。你不能一次给它一个完整的 .java 文件,但你可以给它单个函数的代码字符串并将它们添加到同一个 CtClass 对象,它变成字节码,然后变成 java.lang.Class。
I just released version 0.1 of GigaLineCompile, which uses Javassist (compiler) and Beanshell (interpreter) together and gives you control over which code to optimize and when. In later versions, it will change between Javassist and Beanshell at a smaller granularity so if you have many strings of code that share some substrings, the substrings will be compiled and the other parts run in beanshell. Its mostly useful for artificial intelligence that generates Java code, but its also an alternative to Clojure or the extremes of Javassist/Beanshell alone.
我刚刚发布了 GigaLineCompile 的 0.1 版,它结合使用了 Javassist(编译器)和 Beanshell(解释器),让您可以控制优化哪些代码以及何时优化。在以后的版本中,它会以更小的粒度在 Javassist 和 Beanshell 之间进行更改,因此如果您有许多共享某些子字符串的代码字符串,则这些子字符串将被编译,而其他部分将在 beanshell 中运行。它主要用于生成 Java 代码的人工智能,但它也是 Clojure 或 Javassist/Beanshell 的极端的替代品。
Javassist, Beanshell, and GigaLineCompile can be downloaded (with source) here: http://sourceforge.net/projects/gigalinecompile
Javassist、Beanshell 和 GigaLineCompile 可以在这里下载(带源代码):http: //sourceforge.net/projects/gigalinecompile
回答by abstract
here is java compilation utility, you can compile your code at runtime without any bytecode tool
这是java编译实用程序,您可以在运行时编译代码而无需任何字节码工具