0

My intentions are to ask the user to write down some code in a TextArea, then see if that code compiles, and if it does, print out the results to another TextArea, which acts like a console.

Edit:Solving this via online compilers is the priority.

To accomplish this, I've tried using online compilers (i.e. compilerjava.net) and used the library HtmlUnit, but the library came in with a lot of errors, especially when reading the JavaScript code and returned me pages of 'warnings' that increase the compile & run time for about 20 seconds. I will leave the code below with explanations if anyone has intentions about trying to fix it.

I've also tried using the JavaCompiler interface, which did succeed in compiling, but under the conditions that I have provided the exact location of the .java file, which is something I have to create using the information I get from the TextArea. So again, a dead end.

I decided to come back to online compilers, since if I can manage to just return the data from the compiled program, I am set. The only issue is I haven't yet found an online compiler that allows a user to access its fields via Java code ( since its something too specific). I would appreciate any help on this if anyone can provide a way to send and retrieve data from an online compiler.

Here is the code using the HtmlUnit library, on the site 'compilerjava.net'. It is so close to working that the only 2 issues I have is that,

1) Run-time is too long.

2) I cannot access the console output of the code. Reasoning is that, when you hit 'compile', the output text-area's text turns into "executing". After a few seconds, it turns into the output of the code. When I try to access it, the data I retrieve is always "executing" and not the desired output. Here is the code;

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();
    }
}
}

As I said, this final code System.out.println( resultArea.getText()); prints out "executing", which implies that I have succeeded in pressing the compile button on the webpage via code.

So after this long wall of text, I'm either looking for a way to fix the code I presented, which is so darn close to my answer but not quite, or just an entirely different solution to the problem I presented at the beginning.

P.S. Maven is the last hope.

  • [As a possible example](http://stackoverflow.com/questions/25090434/how-to-compile-and-run-inside-the-java-program/25090801#25090801) and [another example](http://stackoverflow.com/questions/15218892/running-a-java-program-from-another-java-program/15220419#15220419) – MadProgrammer May 09 '17 at 22:40
  • @MadProgrammer I wasn't clear enough maybe, I'm not trying to run 'another' java program from a java program. I am well aware of the console commands of javac Class.java and java Class, I am trying to see if I can compile and run this string. Clearly you have posted without reading the whole thing. –  May 09 '17 at 22:44
  • @ibrahimmahrir As I said, HtmlUnit failed to read Javascript code. People who know about Javascript could help me with this. Thanks for not reading and being a classic SOF user. –  May 09 '17 at 22:46
  • 1
    @MertDuman Look closely at the examples, they compile source code, they just happen to also run it. The first example makes use of the `JavaCompiler` class. *"I am trying to see if I can compile and run this string"* - Do you mean JavaScript or Java source code, because I'm pretty sure you can't do that with Java code, you'll need a fully realised class, you could generate a wrapper class to wrap around the `String` though – MadProgrammer May 09 '17 at 22:48
  • @MadProgrammer I have managed that, but I can't afford to create a new class every time the user writes down some code. That's the main problem with this. –  May 09 '17 at 22:51
  • 2
    I happen to have a working example I've been using - it compiles and runs in memory from a string here: https://pastebin.com/eeSCMvQb Don't remember where I got it to be honest. Like @MadProgrammer suggested, you can supply some logic to wrap this with a class & method. My favorite way to implement this is to also force the class to implement an interface so you don't have to use reflection to invoke methods - you can just type cast when you load the class and create instance manually then just call the method on the interface instance. This example is actually quite fast. – Preston Garno May 10 '17 at 00:43
  • 1
    @MertDuman https://github.com/google/compile-testing check this library out - it does all of the heavy lifting for you – Preston Garno May 10 '17 at 00:46

0 Answers0