从字符串编译Java源代码? [英] Compile Java source code from a string?

查看:251
本文介绍了从字符串编译Java源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种运行Java程序编译Java源代码(作为字符串传递)的方法?

Is there a way for a running Java program to compile Java source code (passed as a string)?

Class newClass = Compiler.compile ("class ABC { void xyz {etc. etc. } }");

理想情况下,传入的源代码引用的任何类都将由程序的类装载器解析。

Ideally, any classes referenced by the passed-in source code would be resolved by the program's class loader.

有这样的存在吗?

推荐答案

请查看 JavaCompiler 类和 javax.tools 包中的其他类。

Sure. Have a look at the JavaCompiler class and the other classes in the javax.tools package.

从Java 1.6开始。

They've been around since Java 1.6.

这里有一些示例代码。

(如@Sergey Tachenov在注释中指出,它需要JDK作为必要的工具安装JDK文件但不是JRE。)

(As pointed out by @Sergey Tachenov in the comments, it needs JDK to be installed as the necessary tools.jar file comes with JDK but not JRE.)

这篇关于从字符串编译Java源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆