运行一个简单的文本文件作为Java [英] Run a simple text file as Java

查看:102
本文介绍了运行一个简单的文本文件作为Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的.txt文件,里面有纯Java代码,如

I have a simple .txt file which has pure Java code inside it like

   public class C {
      public static void main(String[] args ) {
        System.out.println("This is executed");
        }
    }

文件命名为C.txt。现在我想编写Java代码,它将读取C.txt中的代码,并将编译和运行读取的代码作为纯Java文件。注意,我可以很容易地将C.txt重命名为C.java并编译并手动运行代码。但是,这不是我的意图。我想要读取.txt文件,并直接执行代码。

The file is named C.txt. Now I want to write Java code that will read the code in C.txt and will compile and run the read code as a pure Java file. Note, I can easily rename C.txt to C.java and compile and run the code manually. However, this is not my intention. I want to read the .txt file as is and execute the code directly. Is this possible somehow?

推荐答案

我想我会从 BeanShell ,它允许你编译和执行一个字符串中的Java源代码。

I think I'd start with BeanShell, which allows you to compile and execute Java source held in a string.

这篇关于运行一个简单的文本文件作为Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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