使用 Java 编译器 API,无需安装 JDK

发布于 2024-10-31 15:47:25 字数 442 浏览 5 评论 0原文

大家好 我正在编写一些软件,允许用户在我的软件包中创建自己的 Java 类以供特定用途。显然,我的软件需要能够调用 Java 编译器来编译用户生成的类以在我的程序中使用。但是,我不想要求用户下载并安装整个 JDK,以便他们可以访问 javac Java 编译器。据我所知,Jave 6 中有一个新的 Java 编译器 API,但即便如此,仅拥有 JRE 而没有 JDK 的用户在尝试实例化 Java 编译器工具时也会得到一个 null 对象。

那么,让我的程序能够编译 Java 类,同时要求最终用户在其计算机上安装 JRE 的最佳方法是什么?如果这是不可能的,我需要在用户计算机上安装的最小库/jar 文件集是什么?

我想另一种可能性可能是使用 JWS (javaws) 通过网络启动应用程序。在这种情况下,我的软件是否可以不需要JDK(我认为主要是tools.jar)?我是否必须以某种方式将tools.jar 与我的软件捆绑在一起?

Hello All
I am writing some software that will allow users to create their own Java classes for a specific use in my software package. Obviously, my software will need to be able to invoke a Java compiler to compile the user-generated classes for use in my program. However, I do not want to require users to download and install the entire JDK just so they can have access to the javac Java compiler. I understand that in Jave 6 there is a new Java Compiler API but even then, users with just the JRE and not the JDK will get a null object when they try to instantiate the Java compiler tool.

So, what is the best way to enable my program to compile Java classes while requiring the end user to just have the JRE installed on their machines? If this is not possible, what is the minimal set of libraries/jar files I would need to install on the user machine?

I suppose another possibility might be to use JWS (javaws) to launch the app over the web. In this case, is it possible for my software to not require the JDK (mainly tools.jar I think)? Would I somehow have to bundle tools.jar with my software?

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

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

发布评论

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

评论(4

哆兒滾 2024-11-07 15:47:25

您可以使用 Eclipse 中的独立 Java 编译器。它不需要安装 JDK,并且它是您可以在项目中使用的单个 JAR 文件。这是 Eclipse 本身内部使用的编译器,但它也可以集成到其他程序中。您可以在 http://download.eclipse.org/eclipse/downloads/drops/R-3.6.2-201102101200/index.php" rel="noreferrer">http:// /download.eclipse.org/eclipse/downloads/drops/R-3.6.2-201102101200/index.php,查找“查找 JDT Core Batch Compiler”。文档位于 http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm

您还可以使用 Janino,它还可以编译比完整 Java 类更小的单元:块、表达式。但它不是完整的编译器,它不支持用户使用的 Java 语言的所有功能(泛型、注释、枚举……)

You can use standalone Java compiler from Eclipse. It doesn't need JDK installed, and it's single JAR file you can use in your project. This is the compiler that is used inside Eclipse itself, but it can be integrated into other programs too. You can find current latest stable version at http://download.eclipse.org/eclipse/downloads/drops/R-3.6.2-201102101200/index.php, look for Look for JDT Core Batch Compiler. Documentation is available at http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_api_compile.htm

You can also use Janino, which can compile also smaller units than full Java classes: blocks, expressions. It's not full compiler though, it does not support all features of Java language that your users use (generics, annotations, enums, ...)

墨洒年华 2024-11-07 15:47:25

要使用java编译器,您需要将tools.jar包含到您的应用程序中(例如,想要加载编译器的类加载器必须可以访问它 - 最容易由系统类加载器访问)。

To use the java compiler, you need to include tools.jar into your application (e.g. it has to be reachable by the classloader who wants to load the compiler - most easily by the System class loader).

落叶缤纷 2024-11-07 15:47:25

也许 http://asm.ow2.org/ 这会有用吗?动态生成字节码?

Maybe http://asm.ow2.org/ this will be usefull? To generate bytecode on fly?

野侃 2024-11-07 15:47:25

听起来更好的解决方案是使用一些可以在 JVM 中运行而无需编译的脚本语言。

Java Scripting API 可能是对你有用。我确信还有其他选择。

It sounds like a better solution would be to use some scripting language that can run within the JVM without having to be compiled.

Possible the Java Scripting API would be of use to you. I'm sure there are other options as well.

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