如何在运行时编译和部署 java 类?

发布于 2024-07-25 18:16:47 字数 724 浏览 3 评论 0原文

我正在编写一个规则引擎,该引擎执行由条件构造确定的简单分配。 规则采用XML格式是该项目的先决条件。 我已将 XML 模式建模为类似于简单的代码块。 我希望解析 XML,然后将其转换为 Java 代码。 然后我希望在运行时编译(并运行)此代码。 这样做意味着我的规则引擎不再充当解释器,而是执行本机 Java 字节代码。

我已经弄清楚了解析阶段和或多或少的 Java 代码生成阶段。 我现在想弄清楚最后一个阶段 - 运行时编译阶段。

按照此线程: 编译为 java 字节码(不使用 Java) 我已经意识到以下可能的解决方案:

我会喜欢这些的比较以及解决 Java 在运行时阶段编译的其他建议。

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.

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.

Following this thread: Compile to java bytecode (without using Java) I have become aware of the following possible solutions:

I would love a comparison of these as well as other suggestions for solving the Java compile at runtime phase.

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

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

发布评论

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

评论(5

梦毁影碎の 2024-08-01 18:16:47

您可以将其转换为 Clojure 代码,然后 Clojure 编译器会将其转换为字节码。

You could transform it into Clojure code, and the Clojure compiler will turn it into bytecode for you.

清风疏影 2024-08-01 18:16:47

省去麻烦,使用这里提到的 BeanShell 执行文本文件中给出的 java 代码

什么是 BeanShell?

BeanShell 是一个小型、免费、可嵌入的
带对象的 Java 源解释器
脚本语言的特点,书面
在爪哇。 BeanShell 动态
执行标准 Java 语法并且
使用通用脚本对其进行扩展
便利性,例如松散类型,
命令和方法闭包,例如
Perl 和 JavaScript 中的那些。

您可以交互地使用 BeanShell
用于 Java 实验和调试
以及扩展您的应用程序
以新的方式。 Java 脚本可以提供帮助
本身就多种多样
应用包括快速
原型设计、用户脚本扩展、
规则引擎、配置、测试、
动态部署、嵌入式系统、
甚至 Java 教育。

BeanShell 很小并且可以嵌入,所以
你可以从 Java 调用 BeanShell
执行Java代码的应用程序
在运行时动态或提供
应用程序的可扩展性。
或者,您可以使用独立的
操作 Java 的 BeanShell 脚本
应用程序; 使用 Java
动态对象和 API。 自从
BeanShell是用Java编写并运行的
在与您的应用程序相同的虚拟机中,
您可以自由地传递引用
“活动”对象进入脚本并返回
将它们作为结果。

简而言之,BeanShell 是动态的
解释型 Java,加上脚本
语言和灵活的环境都
卷成一个干净的包装。

Save yourself the hassle and use BeanShell as alluded to here 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.

冰雪之触 2024-08-01 18:16:47

Groovy、BeanShell 或任何其他基于 JVM 的脚本语言都具有在运行时注入、修改、添加和运行代码的功能。 实际上所有脚本语言都是解释性的,所以实际上它们并不是在运行时编译的。

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.

灵芸 2024-08-01 18:16:47

Javassist 几乎是一个用 Java 编写的完整 Java 编译器,并且它完全由 Java 制成。 您不能一次给它一个完整的 .java 文件,但您可以给它各个函数的代码字符串,并将它们添加到同一个 CtClass 对象中,该对象变成字节码,然后变成 java.lang.Class。

我刚刚发布了 GigaLineCompile 0.1 版本,它结合使用 Javassist(编译器)和 Beanshell(解释器),让您可以控制优化哪些代码以及何时优化。 在以后的版本中,它将以更小的粒度在 Javassist 和 Beanshell 之间进行更改,因此如果您有许多共享某些子字符串的代码字符串,则将编译子字符串,而其他部分将在 Beanshell 中运行。 它主要用于生成 Java 代码的人工智能,但它也是 Clojure 或 Javassist/Beanshell 极端的替代方案。

Javassist、Beanshell 和 GigaLineCompile 可以在此处下载(包含源代码):
http://sourceforge.net/projects/gigalinecompile

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.

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.

Javassist, Beanshell, and GigaLineCompile can be downloaded (with source) here:
http://sourceforge.net/projects/gigalinecompile

只涨不跌 2024-08-01 18:16:47

您可以分叉这样的进程

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 remember to create a separate thread for it (not created here).
log.debug("PROCESS Output Stream: " + p.getInputStream());
log.debug("PROCESS Error Stream: " + p.getErrorStream());

p.waitFor(); // Wait till the process is finished

并可以编译和运行它。

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 remember to create a separate thread for it (not created here).
log.debug("PROCESS Output Stream: " + p.getInputStream());
log.debug("PROCESS Error Stream: " + p.getErrorStream());

p.waitFor(); // Wait till the process is finished

and can compile and run it.

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