gpt4 book ai didi

java - 从 .txt 文件编译 .class 文件

转载 作者:行者123 更新时间:2023-12-01 12:59:06 25 4
gpt4 key购买 nike

我正在编写一款游戏,玩家通过更改关卡本身内对象的源代码来导航谜题。为了做到这一点,我有一个扩展 JPanel 的类,它将用播放器编辑后的源代码重写。我的问题是如何使用 java 实用程序或使用 beanshell 从文本文件编译类文件?文本文件包含纯java,但游戏玩家可能会导致编译或运行时错误(例如无限循环),我希望能够捕获所述错误并提醒玩家。

最佳答案

听起来是个好主意,但是动态编译类并让它们执行需要大量的工作。有一个库叫 Janino (用他们自己的话说):

Janino is a super-small, super-fast Java™ compiler. Not only can it compile a set of source files to a set of class files like JAVAC, but also can it compile a Java™ expression, block, class body or source file in memory, load the bytecode and execute it directly in the same JVM.

通过将与实际编译和运行动态代码相关的工作委托(delegate)给 Janino,您可以专注于实际的游戏逻辑。

关于java - 从 .txt 文件编译 .class 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23660817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com