Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编译和运行在TextArea中输入的字符串?_Java_Compilation - Fatal编程技术网

Java 如何编译和运行在TextArea中输入的字符串?

Java 如何编译和运行在TextArea中输入的字符串?,java,compilation,Java,Compilation,我的意图是让用户在一个TextArea中写下一些代码,然后查看该代码是否编译,如果编译成功,则将结果打印到另一个TextArea,它的作用类似于控制台 编辑:优先考虑通过在线编译器解决此问题 为了实现这一点,我尝试使用在线编译器(即compilerjava.net)并使用了HtmlUnit库,但该库出现了很多错误,特别是在读取JavaScript代码时,并返回了几页“警告”,这会增加编译和运行时间约20秒。如果有人打算修复它,我将在下面留下代码并给出解释 我也尝试过使用JavaCompiler接

我的意图是让用户在一个TextArea中写下一些代码,然后查看该代码是否编译,如果编译成功,则将结果打印到另一个TextArea,它的作用类似于控制台

编辑:优先考虑通过在线编译器解决此问题

为了实现这一点,我尝试使用在线编译器(即compilerjava.net)并使用了HtmlUnit库,但该库出现了很多错误,特别是在读取JavaScript代码时,并返回了几页“警告”,这会增加编译和运行时间约20秒。如果有人打算修复它,我将在下面留下代码并给出解释

我也尝试过使用JavaCompiler接口,它确实成功地进行了编译,但前提是我提供了.java文件的确切位置,这是我必须使用从TextArea获得的信息创建的。同样,这是一条死胡同

我决定回到在线编译器上来,因为如果我能设法从编译后的程序返回数据,我已经准备好了。唯一的问题是我还没有找到允许用户通过Java代码访问其字段的在线编译器(因为它太具体了)。如果有人能提供从在线编译器发送和检索数据的方法,我将不胜感激

下面是使用“compilerjava.net”网站上的HtmlUnit库的代码。这是如此接近工作,我只有两个问题是

1) 运行时间太长

2) 我无法访问代码的控制台输出。原因是,当您点击“编译”时,输出文本区域的文本变成“正在执行”。几秒钟后,它变成代码的输出。当我试图访问它时,我检索的数据总是“正在执行”,而不是期望的输出。这是代码

public class Test {
public static void main(String[] args) {
    try {
        // Prevents the program to print thousands of warning codes.
        java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
        java.util.logging.Logger.getLogger("org.apache.http").setLevel(java.util.logging.Level.OFF);

        // Initializes the web client and yet again stops some warning codes.
        WebClient webClient = new WebClient( BrowserVersion.CHROME);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode( false);
        webClient.getOptions().setThrowExceptionOnScriptError( false);
        webClient.getOptions().setJavaScriptEnabled( true);
        webClient.getOptions().setCssEnabled( true);

        // Gets the html page, which is the online compiler I'm using.
        HtmlPage page = webClient.getPage("https://www.compilejava.net/");

        // Succesfully finds the form which has the required buttons etc.
        List<HtmlForm> forms = page.getForms();
        HtmlForm form = forms.get( 0);

        // Finds the textarea which will hold the code.
        HtmlTextArea textArea = form.getTextAreaByName( "code");
        // Finds the textarea which will hold the result of the compilation.
        HtmlTextArea resultArea = page.getHtmlElementById( "execsout");

        // Finds the compile button.
        HtmlButtonInput button = form.getInputByName( "compile");
        System.out.println( button.getValueAttribute());

        // Simple code to run.
        textArea.setText( "public class HelloWorld\n" +
                "{\n" +
                "  // arguments are passed using the text field below this editor\n" +
                "  public static void main(String[] args)\n" +
                "  {\n" +
                "    System.out.print( \"Hello\");\n" +
                "  }\n" +
                "}");

        // Compiles.
        button.click();

        // Result of the compilation.
        System.out.println( resultArea.getText());

    } catch ( Exception e) {
        e.printStackTrace();
    }
}
}
公共类测试{
公共静态void main(字符串[]args){
试一试{
//防止程序打印数千个警告代码。
getLogger(“com.gargoylesoftware”).setLevel(java.util.logging.Level.OFF);
java.util.logging.Logger.getLogger(“org.apache.http”).setLevel(java.util.logging.Level.OFF);
//初始化web客户端,然后再次停止一些警告代码。
WebClient WebClient=新的WebClient(BrowserVersion.CHROME);
webClient.getOptions().SetThroweExceptionOnFailingStatusCode(false);
webClient.getOptions().SetThroweExceptionOnScriptError(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setCssEnabled(true);
//获取html页面,它是我正在使用的联机编译器。
HtmlPage=webClient.getPage(“https://www.compilejava.net/");
//成功找到具有所需按钮等的窗体。
列表表单=page.getForms();
HtmlForm form=forms.get(0);
//查找将保存代码的文本区域。
HtmlTextArea textArea=form.getTextAreaByName(“代码”);
//查找将保存编译结果的textarea。
HtmlTextArea resultArea=page.getHtmlElementById(“execsout”);
//查找“编译”按钮。
HTMLButtoniInputButton=form.getInputByName(“编译”);
System.out.println(button.getValueAttribute());
//运行简单的代码。
textArea.setText(“公共类HelloWorld\n”+
“{\n”+
“//使用此编辑器下面的文本字段传递参数\n”+
“公共静态void main(字符串[]参数)\n”+
“{\n”+
“System.out.print(\“Hello\”);\n”+
“}\n”+
"}");
//编译。
按钮。单击();
//汇编结果。
System.out.println(resultArea.getText());
}捕获(例外e){
e、 printStackTrace();
}
}
}
正如我所说,最后的代码
System.out.println(resultArea.getText())
打印出“executing”,这意味着我已经通过代码成功地按下了网页上的compile按钮

因此,在这一长串的文本之后,我要么在寻找一种方法来修复我提出的代码,这与我的答案非常接近,但并不完全相同,要么只是一个完全不同的解决方案来解决我在开始时提出的问题


p.S.Maven是最后的希望。

和@MadProgrammer我还不够清楚,也许,我没有试图从java程序运行“另一个”java程序。我很清楚javac Class.java和java Class的控制台命令,我试图看看是否可以编译和运行这个字符串。很明显,你没有阅读全部内容就发布了。@MertDuman仔细看这些示例,它们编译源代码,只是碰巧也运行了它。第一个示例使用
JavaCompiler
类。“我想看看我是否可以编译和运行这个字符串”-你是指JavaScript还是Java源代码,因为我很确定Java代码不能做到这一点,你需要一个完全实现的类,您可以生成一个包装器类来包装
字符串
,尽管我碰巧有一个我一直在使用的工作示例-它从这里的字符串编译并在内存中运行:老实说,不记得从哪里得到它。就像@MadProgrammer建议的那样,您可以提供一些逻辑,用一个类和方法来包装它。我最喜欢的实现方法是强制类实现一个接口,这样就不必使用反射来调用方法——加载类并手动创建实例时,只需键入cast,然后在接口实例上调用该方法即可。这个例子实际上相当快。@MertDuman检查一下这个库-它为您完成所有繁重的工作