Online Compiler

Status
Not open for further replies.

bhags21

Right off the assembly line
I need help in building my application.
My application allows user to enter C/C++/java code.when they add their code they should be able to check the code whether it runs.So it requires a online compiler so that i can send the code to tat compiler and get the output.
or if there is any jar file which can be attached with the proj is also fine.
My application is web application..i'm building it with servlets and JSP..

Can anyone give me some suggestion.*www.thinkdigit.com/forum/images/icons/icon7.gif. Atleast if it compiles C code its enough..
 

chandru.in

In the zone
Here's a quickie online C compiler. It assumes that your server has gcc installed.

JSP Page:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"*www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Online Compiler</title>
    </head>
    <body>
        <h1>Enter code</h1>
        <form method="post" action="CompilerServlet">
            <textarea name="code" cols="80" rows="25"></textarea>
            <br>
            <input type="submit" value="Compile Code"></input>
        </form>
    </body>
</html>

Servlet Code:
Code:
public class CompilerServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        File sourceFile = File.createTempFile("src", ".c");
        File execFile = File.createTempFile("exec", ".out");

        // Write to temporary source file
        PrintWriter sourceWriter = new PrintWriter(sourceFile);
        sourceWriter.println(request.getParameter("code"));
        sourceWriter.close();

        // Compile
        Process compilerProc = Runtime.getRuntime().exec("gcc -Wall -o "
                + execFile.getAbsolutePath()
                + " " + sourceFile.getAbsolutePath());

        // Capture Compiler output
        BufferedReader compilerReader = new BufferedReader(new InputStreamReader(compilerProc.getErrorStream()));
        out.println("Compiler output:\n");
        String line;
        while ((line = compilerReader.readLine()) != null) {
            out.println(line);
        }

        compilerReader.close();
        sourceFile.delete();
        execFile.delete();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

Warning: This is just a quick hack. The servlet code may leave streams unclosed at times.
 

QwertyManiac

Commander in Chief
I wouldn't recommend you do this without going through the code and eliminating harmful procedures. Something like what *codepad.org does.
 

chandru.in

In the zone
^^ The code I provided just compiles and does not execute the submitted code (unlike codepad.org), so it basically is just a syntax check before posting and doesn't post any risk, even if the submitted source code is malicious.
 
Status
Not open for further replies.
Top Bottom